Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除数组中的冗余值_Java - Fatal编程技术网

Java 删除数组中的冗余值

Java 删除数组中的冗余值,java,Java,我不知道为什么RemovedUpplicates方法拒绝实际删除非唯一值。我不确定问题是大小增加还是我的方法调用 // post: places the value in the correct place based on ascending order public void add(int value) { size++; if (size == 1) { elementData[0] = value; } else {

我不知道为什么RemovedUpplicates方法拒绝实际删除非唯一值。我不确定问题是大小增加还是我的方法调用

// post: places the value in the correct place based on ascending order
public void add(int value) {
    size++;
    if (size == 1) {
        elementData[0] = value;
        } else {
            int position =  Arrays.binarySearch(elementData, 0, size - 1, value);
            if (position < 0 ) {
            position = (-position) - 1;
        }
            for (int i = size - 1; i > position; i--) {
            elementData[i] = elementData[i - 1];
        }
            elementData[position] = value;
        }
    if (unique) {
        removeDuplicates();
    }
}

//post: removes any duplicate values from the list
private void removeDuplicates() {
    for(int i = size - 1; i > 0; i--) {
        if (elementData[i] == elementData[i - 1]){
            remove(i - 1);
        }
    }
}
//post:根据升序将值放置在正确的位置
公共void add(int值){
大小++;
如果(大小==1){
elementData[0]=值;
}否则{
int position=Arrays.binarySearch(elementData,0,size-1,value);
如果(位置<0){
位置=(-位置)-1;
}
对于(int i=大小-1;i>位置;i--){
elementData[i]=elementData[i-1];
}
elementData[位置]=值;
}
如果(唯一){
移除重复项();
}
}
//post:从列表中删除任何重复的值
私有无效删除副本(){
对于(int i=size-1;i>0;i--){
if(elementData[i]==elementData[i-1]){
移除(i-1);
}
}
}
试试这个

//将其转换为list,因为我们需要list对象来创建 //设置对象。集合是一个集合对象,不能具有 //重复的值,因此通过将数组转换为集合 //重复的值将被删除

List<String> list = Arrays.asList(data);
Set<String> set = new HashSet<String>(list);

System.out.print("Remove duplicate result: ");

//
// Create an array to convert the Set back to array.
// The Set.toArray() method copy the value in the set to the
// defined array.
//
String[] result = new String[set.size()];
set.toArray(result);
for (String s : result) {
    System.out.print(s + ", ");
List List=Arrays.asList(数据);
Set Set=新哈希集(列表);
系统输出打印(“删除重复结果:”);
//
//创建一个数组以将集合转换回数组。
//方法将集合中的值复制到
//定义的数组。
//
字符串[]结果=新字符串[set.size()];
设置到阵列(结果);
for(字符串s:结果){
系统输出打印(s+“,”);
@user98643-

Jano的建议是正确的:最好的解决方案是简单地使用适当的数据结构,例如

建议:

1)一般来说,总是考虑使用容器这样的“列表”优先于数组

2) 通常,查找已经具有所需大多数属性的容器

3) 在这种情况下,A)需要对所有元素进行排序,B)每个元素必须是唯一的

一棵树很合身

伊姆霍


remove(i-1);这是一种方法吗???“elementData”的类型是什么?如果它是一个对象,你不能使用“==”。elementData是一个整数数组。remove是一种方法,它接受一个索引,删除该索引处的值,并将所有值滑动到右边一个空格左一个空格。使用集合(映射或集合)对于这样的需求,它将比数组更丰富。最好不要在一开始就插入重复的值。还有,为什么要在有SortedSet、TreeSet等时重新发明轮子?