【发布时间】:2015-05-17 22:33:58
【问题描述】:
我正在尝试制作数组列表的数组列表。我的代码到处都是,我只是在添加东西并尝试错误尝试。最后我想做的是,说我有一辆车......它有速度,英里,mpg,如果他们想买它,把它添加到arraylists的arraylist中,然后就能得到一个拥有汽车的清单,只是为了让您了解我正在尝试做的事情。 idk 如果有帮助,或者也许有更好的方法来做到这一点。我只需要内部数组迭代的帮助,其余的我可以弄清楚。现在它只是迭代主数组列表并显示其中的所有内容看起来像一个数组。我宁愿使用 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();
}
}
【问题讨论】:
-
查看您的
gg()方法:您声明一个新的ArrayList并添加到它而不是字段。没有我在哪里看到你添加到字段listOfSomething1,这可能就是为什么迭代它会导致任何结果 -
请正确命名您的方法和变量。它可以帮助人们看到您想要实现的目标,从而使他们能够为您提供更好的帮助。
-
@Vince Emigh 抱歉,我是从我的主班添加的,只是把 Thelist 放了,抱歉,我会添加一个编辑,让其他人都清楚
-
@mike 我知道只是让它进入工作模式,我一直在左右改变它,只使用最快的,抱歉!