[英]ArrayList of ArrayLists adding and retrieving elements
我有一个 Integer ArrayList(mainList) 里面有 Integer Arrays(subList),我正在尝试将整数数组元素添加到 mainList 并在以后显示它们。 将 subLists 添加到 mainList 并显示 subList 中的所有元素。
2 subLists = {1,2,4},{3,2,1}
mainList =[{1,2,4},{3,2,1}]
Display : 1,2,4,3,2,1
以下是我尝试将 subLists 添加到 mainList 的方式
//Adding elements
ArrayList<ArrayList<Integer>> mainList = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> subList = new ArrayList<Integer>();
for(int i=0;i<10;i++) {
for(int j=i+1;j<10;j++){
//Do something and add elements to subList
subList.add(element[j]) }
mainList.add(subList);
// When I clear subList mainList is also getting cleared. I want to add the elements of subList to mainList. I was able to do it with loops but how to do this way
subList.clear();
}
//Printing, How do I get the elements from mainList which will have subLists
for(Integer i:mainList){
System.out.println(i);
}
}
你的代码
ArrayList<Integer><ArrayList<Integer>> mainList = new ArrayList<ArrayList<Integer>>();
无法编译,请参阅以下代码中的编辑:
import com.sun.deploy.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> mainList = new ArrayList<ArrayList<Integer>>();
Integer[] sub1 = {1,2,4};
Integer[] sub2 = {3,2,1};
ArrayList<Integer> subList1 = new ArrayList<>(Arrays.asList(sub1));
ArrayList<Integer> subList2 = new ArrayList<>(Arrays.asList(sub2));
mainList.add(subList1);
mainList.add(subList2); //[[1, 2, 4], [3, 2, 1]]
ArrayList<Integer> intValues = new ArrayList<>();
for(ArrayList<Integer> inner: mainList){
intValues.addAll(inner); // add inner list elements to Integer list
}
List<String> strings = intValues.stream().map(Object::toString)
.collect(Collectors.toList()); // converts to string list
System.out.println(StringUtils.join(strings, ",")); // prints comma separated string
}
}
输出:
1,2,4,3,2,1
我的解释在代码注释中。
所以,你有一个数组列表,一个数组列表,一个整数。 mailList 是 subList 的数组列表。 subList 是整数的数组列表。 因此,要获得一个元素,您必须获得一个元素。 我会给你看。
ArrayList temp = mailList.get(i);
temp.get(j);
因此,使用高级 for 循环,您可以访问每个元素。
for(ArrayList i:mailList)
for(Integer j:i)
System.out.print(j);
该代码将打印二维数组列表中的所有整数。 我现在将使用普通的 for 循环对其进行编码。
for(int i = 0; i < mailList.length; i++){
ArrayList subList = mailList.get(i);
for(int j = 0; j < subList.length; j++){
System.out.print(subList.get(j));
}
}
该代码将执行完全相同的操作。
所以,总而言之,你从数组列表的数组列表中获取数组列表,然后从数组列表中的数组列表中获取整数。 希望这可以帮助! :)
(是的,我知道 array-list-ception 有点令人困惑,但这是我能想到的唯一解释方法)
我有办法!
class Main {
public static void main(String[] args) {
ArrayList < ArrayList < Integer >> mainList = new ArrayList < ArrayList < Integer >> ();
ArrayList < Integer > subList = new ArrayList < Integer > ();
for (int i = 0; i < 10; i++) {
subList = new ArrayList < Integer > ();
for (int j = i + 1; j < 10; j++) {
subList.add(element[j])
}
mainList.add(subList);
}
}
}
这是构建异构 ArrayList 的 ArrayList 的通用解决方案,例如在 SQL 查询结果集中可能遇到的情况。
package com.mwycombe.arraylistdatatest;
import java.util.ArrayList;
/**
*
* @author MWycombe
*/
public class ArrayListDataTest {
ArrayList<Object> tableData;
public ArrayListDataTest(){
int i, j;
// set up trasnposed table of data
tableData = new ArrayList<>();
// set up 6 rows with 3 cols - each column is the same.
ArrayList<Integer> col1 = new ArrayList<>();
ArrayList<String> col2 = new ArrayList<>();
ArrayList<Float> col3 = new ArrayList<>();
// how build 6 arbitrayr entries for each column and add to the tableData array list
for (i = 0; i < 6; i++){
col1.add(i );
col3.add((float)i + i/10);
}
for (i = 0; i < 6; i++) {
col2.add(String.valueOf(i));
}
tableData.add(col1);
tableData.add(col2);
tableData.add(col3);
System.out.println ("Array lists built.");
System.out.println ("Size tableData, col1, col2, col3");
//print out class of each column - 1st row entry cell
for (i=0; i<3; i++){
System.out.println("Col type " + i + " " + getColumnClass(i));
}
}
private Class getColumnClass(int c){
System.out.println("Which col? " + c);
return getValueAt(0,c).getClass();
}
private Object getValueAt(int row, int col) {
System.out.println(("in getValue row: " + row + " col: " + col));
return ((ArrayList)(tableData.get(col))).get(row);
}
public static void main(String[] args) {
ArrayListDataTest aldt = new ArrayListDataTest();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.