Java 如何将 ArrayUtils 用于对象数组,它不会删除数组的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26360865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use ArrayUtils for array of objects, it doesn't delete the content of an array
提问by Naufal Yahaya
How to delete the content of an array of objects. If there is other ways to delete a content of an array of objects , please do share.
如何删除对象数组的内容。如果有其他方法可以删除数组对象的内容,请分享。
import java.util.Arrays;
import java.util.Scanner;
import org.apache.commons.lang3.ArrayUtils;
public class Testing {
public static void deleteItem(ItemTracker[] listItems) {
System.out.println("Which item you want to delete? ");
for(int i=0; i < listItems.length; i++) {
if(input.equalsIgnoreCase("Quantity")) {
// Some Code
} else if(input.equalsIgnoreCase("Something"){
ArrayUtils.remove(listItems, i); // This is the part where it should delete .. but it doesnt delete.
}
break;
}
}
}
采纳答案by Tom
Change this
改变这个
ArrayUtils.remove(listItems, i);
to
到
listItems = ArrayUtils.remove(listItems, i);
As you can see in the JavaDoc, the method does not change the argument listItems
, rather it returns a new array with the remaining elements.
正如您在JavaDoc 中看到的,该方法不会更改参数listItems
,而是返回一个包含剩余元素的新数组。
Edit
编辑
You also need to change your deletion method to
您还需要将删除方法更改为
public static ItemTracker[] deleteItem(ItemTracker[] listItems) {
//..
}
So you could return the new array with the remaining elements.
所以你可以用剩余的元素返回新数组。
回答by CoderCroc
This method returns a new arraywith the same elements of the input array except the element on the specified position. The component type of the returned array is always the same as that of the input array.
此方法返回一个新数组,该数组具有与输入数组相同的元素,但指定位置上的元素除外。返回数组的组件类型始终与输入数组的组件类型相同。
So,you should use it like this
所以,你应该像这样使用它
listItems = ArrayUtils.remove(listItems, i);
NOTE
笔记
- Here we have assign the returned array to current
listItem
. - As this method does not change the actual array but returns the changed array after removal same as
#replace
method works forString
.
- 这里我们将返回的数组分配给 current
listItem
。 - 由于此方法不会更改实际数组,而是在删除后返回更改后的数组,与
#replace
方法相同String
。
YES. I agree with
zvdh
I have missed the purpose of your method because I was more concentrated on removal of element.Sorry for that!! as this will not actually change thelistItem
and you need toreturn
the new array which contains the change.
是的。我同意
zvdh
我错过了你的方法的目的,因为我更专注于删除元素。对不起! 因为这实际上不会改变listItem
并且您需要return
包含更改的新数组。
回答by JavaLearner
Store the resulting array.
存储结果数组。
It won't change the original array object.
它不会改变原始数组对象。
listItems = ArrayUtils.remove(listItems, i);
Edit:But for using this method you need the change to return type of your method
编辑:但是要使用此方法,您需要更改方法的返回类型
public static ItemTracker[] deleteItem(ItemTracker[] listItems){
System.out.println("Which item you want to delete? ");
for(int i=0; i < listItems.length; i++) {
if(input.equalsIgnoreCase("Quantity")) {
// Some Code
} else if(input.equalsIgnoreCase("Something"){
listItems = ArrayUtils.remove(listItems, i); // This is the part where it should delete .. but it doesnt delete.
}
break;
}
return listItems;
}
回答by Ilya
In your case usage of ArrayUtils
is incorrect and redundant. You can delete element in next way:
在您的情况下,使用ArrayUtils
是不正确和多余的。您可以通过以下方式删除元素:
// ...
listItems[i] = null;
// array will looks like [o1, o2, null, o3, o4, ...]
// ...
There is no other way without changing method's return type
没有改变方法的返回类型没有其他方法
回答by Krzysztof Jab?oński
Without additional libraries, with temporary list:
没有额外的库,有临时列表:
Element arrayToRemoveFrom[];
Element toRemove; // should be known already
ArrayList<Element> tmpList = new ArrayList<Element>(Arrays.asList(arrayToRemoveFrom));
tmpList.remove(toRemove);
// any other code processing and removing elements
arrayToRemoveFrom = tmpList.toArray(new Arrays[tmlList.size()]);
回答by And
class Arrays
{
public static void main(String[] args)
{
double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5};
java.util.Arrays.sort(numbers);
System.out.print("Ascending order= ");
for (int i = 0; i < numbers.length; i++)
System.out.print(numbers[i] + " ");
System.out.println();
System.out.print("Decending order= ");
for (int i = numbers.length -1; i >= 0; i--)
System.out.print(numbers[i] + " ");
}
}
This solution only displays in reverse order, but it can be changed to reorder the array using the same loop.
此解决方案仅以相反的顺序显示,但可以更改为使用相同的循环对数组重新排序。