我试图制作一个arraylists的arraylist。我的代码到处都是,我刚刚添加了东西并尝试错误试用。最后我要做的就是说,我有一辆车...它有速度,里程,英里每小时,如果他们想买它,就把它添加到arraylists的arraylist,然后能够获取所拥有汽车的清单,只是为了让您了解我想要做的事情。 idk如果它有帮助,或者可能有更好的方法。我只需要内部数组的迭代帮助,其余的我可以搞清楚。现在它只是迭代主要的arraylist并显示那里看起来像一个数组的一切。我宁愿使用for循环然后迭代类,但它如何工作对我有好处。
public class Thelist {
String a ="aa";
String b ="bb";
String c ="cc";
static ArrayList<ArrayList<String>> collection = new ArrayList<ArrayList<String>>();
static int count=0;
ArrayList<String> listOfSomething1 = new ArrayList<String>();
public void gg(){
ArrayList<String> listOfSomething1 = new ArrayList<String>();
listOfSomething1.add(a);
listOfSomething1.add(b);
listOfSomething1.add(c);
collection.add(listOfSomething1);
count++;
}
public void jk(int u){
//this one feel like i can make new arraylist and use for loop for the collection size and iterate each one
//but seems pretty hacky to me
ArrayList<String> lis = new ArrayList<String>();
lis.addAll(collection.get(u));
for(int i=0;i<lis.size();i++ ){
System.out.println("each "+i+" "+lis.get(i));
}
}
public void ll(){
System.out.println(collection.size());
System.out.println(collection.get(0).size());
}
public void ccc(String x,String y, String z){
this.a= x;
this.b=y;
this.c=z;
}
public void remo(int r){
collection.remove(0);
count--;
}
public void getcount(){
System.out.println("the count "+count);
}
public void showall(){
//this one shows all of the arraylist as arrays it looks like to me, I want each element
// feel like i should be able to add another for loop to iterate over listOfSomething1
//which is be add to the collection arraylist
// but it don't work tryed for each too
for(int i=0;i < collection.size();i++){
System.out.println("uoo "+collection.get(i));
}
}
}
public class MainActivity extends Activity {
static int oo = 1;
Thelist ll = new Thelist();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void woow(View v){
String gg = Integer.toString(oo);
ll.ccc("phi"+gg, "phil"+gg, "phily"+gg);
ll.gg();
oo++;
}
public void shoo(View v){
ll.showall();
}
}
答案 0 :(得分:4)
只需使用两个嵌套的for循环。第一个迭代集合,每次迭代产生一个ArrayList<String>
。获取此列表并在第二次迭代中迭代它。为简单起见,您可以在此处使用for-each循环:
for(ArrayList<String> cars : collection) {
for(String car : cars) {
System.out.println(car);
}
}
答案 1 :(得分:0)
public void showall(){
for(int i=0;i < collection.size();i++)
for(int j=0; j< collection.get(i).size();j++)
System.out.println(collection.get(i).get(j));
}