本文共 935 字,大约阅读时间需要 3 分钟。
package java_se;
import java.util.Arrays;
public class Test05 {
public static void main(String[] args) {
int c = 0; int[] a = new int[] { 1, 2, 3, 1, 1, 1 }; RepeatCount rc = new RepeatCount(); NoRepeat b = new NoRepeat(a.length - rc.repeat(a)); b.newArry(a); } }class RepeatCount {
public int repeat(int[] a) {
int count = 0; for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { // System.out.println(a[i]+","+a[j]); if (a[i] == a[j]) { count++; break; } }}
return count;}
}class NoRepeat {
int index = 0; int t = 0; boolean flag = false;public NoRepeat(int index) {
System.out.println(index); this.index = index; }public void newArry(int[] a) {
int[] b = new int[index];for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++) {// 遍历数组a的值是否在b中存在 if (a[i] == b[j]) { flag = true; break; } } if (!flag) {// 只存放在a中有但是在b中没有 的数据 b[t++] = a[i]; } } System.out.println(Arrays.toString(b)); } }转载地址:http://tejxi.baihongyu.com/