簡體   English   中英

從數組列表中獲取數組項

[英]Get array item from 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 錯誤

如果你有一個“普通”數組,你可以簡單地訪問第i個元素,帶有索引,作為myArrayList.get(1).normalArray[i]

如果您需要處理數組,請考慮將其存儲到本地副本中並從中訪問。

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

訪問像,

int first=arr[0];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM