Java 将多个ArrayList转换为一个数组
我需要将arraylist的值放入数组中,以便完成for循环项目。我有一个包含10个arraylist的arraylist,每个数组都有一个或多个整数值:arraylist List=new arraylist; 我使用for循环创建了另外10个数组,现在我需要将10个ArrayList放入一个Java 将多个ArrayList转换为一个数组,java,arrays,arraylist,Java,Arrays,Arraylist,我需要将arraylist的值放入数组中,以便完成for循环项目。我有一个包含10个arraylist的arraylist,每个数组都有一个或多个整数值:arraylist List=new arraylist; 我使用for循环创建了另外10个数组,现在我需要将10个ArrayList放入一个 Integer [] second; 我需要将ArrayList按其放置顺序放入[]数组中,我必须这样做才能完成我的项目。但是由于某些原因,我的for循环(用于将每个arraylist放入数组)不会
Integer [] second;
我需要将ArrayList按其放置顺序放入[]数组中,我必须这样做才能完成我的项目。但是由于某些原因,我的for循环(用于将每个arraylist放入数组)不会打印它们。有什么建议吗?
下面是我的for循环,用于将ArrayList打印到数组中:
for(int i=0; i<lists.siz();i++)
{
second = lists.get(i).toArray(second);
}
导入java.util.ArrayList 导入org.apache.commons.lang3.RandomUtils 公共类数组列表数组{
public static Integer[] IncreaseArraySizeByOneElement(Integer[] oldArray){
int sizeOfOldArray=oldArray.length;
int newSizeOfArray=sizeOfOldArray+1;
Integer[] newArray=new Integer[newSizeOfArray];
for(int x=0;x<sizeOfOldArray;x++){
newArray[x]=oldArray[x];
}
return newArray;
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> ListOfIntArray = new ArrayList<ArrayList<Integer>>();
for (int x = 0; x < 10; x++) {
ArrayList<Integer> ListOfInts = new ArrayList<Integer>();
for (int y = 0; y < 5; y++) {
ListOfInts.add(RandomUtils.nextInt(4800, 7000));
}
ListOfIntArray.add(ListOfInts);
}
System.out.println("There are 10 ArrayList containing each ArrayList 5 elements "+ListOfIntArray);
System.out.println("Let's put now above ArrayList of ArrayList into a single Integer[]");
Integer[] arrayOfMyInts = null;
for (ArrayList<Integer> ListOfInts : ListOfIntArray) {
for (int y = 0; y < 5; y++) {
if(arrayOfMyInts==null){
arrayOfMyInts = new Integer[0];
}
arrayOfMyInts=ArrayListToArray.IncreaseArraySizeByOneElement(arrayOfMyInts);
arrayOfMyInts[arrayOfMyInts.length-1]=new Integer(ListOfInts.get(y));
}
}
System.out.println("Printing all elements Integer[]");
for(int x=0;x<arrayOfMyInts.length;x++)
System.out.println(arrayOfMyInts[x]);
}
}秒未初始化。尝试ArrayNew Integer[0]。上面的代码显示如何创建arrayList类型Integer的arrayList,然后将它们放入Integer的单个数组并打印输出。我希望这对你有帮助
public static Integer[] IncreaseArraySizeByOneElement(Integer[] oldArray){
int sizeOfOldArray=oldArray.length;
int newSizeOfArray=sizeOfOldArray+1;
Integer[] newArray=new Integer[newSizeOfArray];
for(int x=0;x<sizeOfOldArray;x++){
newArray[x]=oldArray[x];
}
return newArray;
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> ListOfIntArray = new ArrayList<ArrayList<Integer>>();
for (int x = 0; x < 10; x++) {
ArrayList<Integer> ListOfInts = new ArrayList<Integer>();
for (int y = 0; y < 5; y++) {
ListOfInts.add(RandomUtils.nextInt(4800, 7000));
}
ListOfIntArray.add(ListOfInts);
}
System.out.println("There are 10 ArrayList containing each ArrayList 5 elements "+ListOfIntArray);
System.out.println("Let's put now above ArrayList of ArrayList into a single Integer[]");
Integer[] arrayOfMyInts = null;
for (ArrayList<Integer> ListOfInts : ListOfIntArray) {
for (int y = 0; y < 5; y++) {
if(arrayOfMyInts==null){
arrayOfMyInts = new Integer[0];
}
arrayOfMyInts=ArrayListToArray.IncreaseArraySizeByOneElement(arrayOfMyInts);
arrayOfMyInts[arrayOfMyInts.length-1]=new Integer(ListOfInts.get(y));
}
}
System.out.println("Printing all elements Integer[]");
for(int x=0;x<arrayOfMyInts.length;x++)
System.out.println(arrayOfMyInts[x]);
}