[英]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.