简体   繁体   English

如何转换ArrayList <Integer> 到int []?

[英]How to convert ArrayList<Integer> to an int[]?

I have an array that is saved in the strings.xml. 我有一个保存在strings.xml中的数组。
I want to get how many items are there in the list, and then put it in another list. 我想获取列表中有多少个项目,然后将其放在另一个列表中。

For example, if the String contains {"One", "Two", "Three"} , 例如,如果String包含{"One", "Two", "Three"}
then I want an int that contains {0,1,2} . 那么我想要一个包含{0,1,2}int

From what I know, the only way to do it is to use the ArrayList , so I could use the add method. 据我所知,唯一的方法是使用ArrayList ,所以我可以使用add方法。

Here is the code I'm using to make this: 这是我用来实现此目的的代码:

String[] names;
ArrayList<Integer> MyList;

for (int i = 0; i < names.length; i++){
  MyList.add(i);
}

But the problem is that I don't know how to change the ArrayList<Integer> into an int[] . 但是问题是我不知道如何将ArrayList<Integer>更改为int[]
How can this be done? 如何才能做到这一点?

Check out ArrayList api 签出ArrayList API

Integer[] stockArr = new Integer[yourList.size()];
stockArr = yourList.toArray(stockArr);

Java Supports Auto un-boxing even though. Java即使支持自动拆箱。

  int[] finalArr = new int[stockArray.length];
 for(int i=0;i<stackArray.length;i++ )
 {
      int[i] = stocArray[i];
  }

If you want to create an int[] array of numbers from 0 up to names.length , then this is how you do it: 如果要创建一个从0到names.lengthint[]数字数组,请按以下步骤操作:

int[] numbers = new int[names.length];
for (int i = 0; i < names.length; i++) {
    numbers[i] = i;
}

For ordinary Object types, you can simply use the ArrayList.toArray() method to convert an ArrayList to an equivalent array of objects. 对于普通的对象类型,您可以简单地使用ArrayList.toArray()方法将ArrayList转换为等效的对象数组。 However, in this question, it appears that you are also looking to perform unboxing of the primitive types, as well. 但是,在这个问题中,您似乎也希望对原始类型进行拆箱。 To do that, you simply need to directly copy: 为此,您只需要直接复制:

 private static int[] convertArrayListToPrimitiveArray(
     ArrayList<Integer> arrayList) {
   int count = arrayList.size();
   int[] result = new int[count];
   for (int i = 0; i < count; i++) {
     result[i] = arrayList.get(i);
   }
   return result;
 }

You can use the function above in your code like this: 您可以像下面这样在代码中使用上面的函数:

ArrayList<Integer> original = ...
int[] equivalentAsArray = convertArrayListToPrimitiveArray(original);

That being said, it is generally preferred/more idiomatic to use the Java collection types over raw arrays in Java code, and doing this conversion is not free (it takes time proportional to the size of the array and it ends up duplicating the amount of memory needed to store the data). 话虽这么说,与Java代码中的原始数组相比,使用Java集合类型通常是首选/惯用的,并且这种转换不是免费的(与数组大小成比例的时间并最终导致重复的数量增加)。存储数据所需的内存)。 So if you can use a Iterable<Integer> , List<Integer> , or ArrayList<Integer> instead of performing this conversion, I'd highly advise that you do so. 因此,如果您可以使用Iterable<Integer>List<Integer>ArrayList<Integer>代替执行此转换,则强烈建议您这样做。 It may be possible for you to simply use the existing collection rather than performing the conversion (and if you need to pass the raw array to a helper or library method, extending the library to support collection types may be the better approach). 您可能可以简单地使用现有集合而不是执行转换(如果需要将原始数组传递给帮助程序或库方法,则扩展库以支持集合类型可能是更好的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM