从arraylist

时间:2015-12-24 06:49:01

标签: java arrays arraylist

我正在修改我的问题以使其更容易理解。 我有以下arraylist

ArrayList<AnotherClass> exmapleName = new ArrayList<AnotherClass>();
// AnotherClass is a class I created, I store objects of the class in this array, the code to this doesn't matter this works.

在exampleName内部,我有不同类型的数据

public AnotherClass (String someString,  int someInt, int anotherInt, int[] intArray, ArrayList<Integer> arrayList)
//Header of the constructor

现在我需要做的是访问和存储arrayList [0]。但是,一旦创建了对象,我就需要这样做,因此arrayList中实际存在一些东西。

这是我尝试但似乎无法正常工作

         AnotherClass test1 = exampleName.get(1);
        ArrayList<Integer> test2 = test1.arrayList;
        int test3 = test2.arrayList[0];
// I broke it down into separate lines to make it more understandable 

编译器错误如下

cannot find symbol
                    int test3 = test2.arrayList[0];
symbol:   variable arrayList
location: variable test2 of type ArrayList<Integer>

1错误

2 个答案:

答案 0 :(得分:2)

如果您使用的是“普通”数组,则只需使用索引i访问myArrayList.get(1).normalArray[i]个元素即可。

如果您需要处理数组,请考虑将其存储到本地副本中并从中进行访问。

int[] numbers = myArrayList.get(1);
for(Integer num : numbers){
  // Process the numbers array.
  System.out.print(num);
}

答案 1 :(得分:0)

 int arr[] = myArrayList.get(1);

访问类似,

int first=arr[0];