簡體   English   中英

我想轉換一個ArrayList <Integer> 是否已將其放入片段的args包中,以便為其他方法鍵入Integer []?

[英]I want to convert an ArrayList<Integer> that was put into the args bundle of a fragment to type Integer[] for an other method?

我想轉換放入片段的args包中的ArrayList<Integer>來鍵入Integer[]這樣我就可以將它傳遞給需要Integer[]類型的方法。 我有

ArrayList<Integer> friends

然后我創建了一個空的Integer數組。

Integer[] bal= new Integer[friends.size()];

我知道我必須用for循環做一些事!

為了使用數組列表中的值填充數組,您可以使用循環(如您提到的'for循環')迭代ArrayList並將其數據放入數組中。

您可以使用以下代碼來完成此任務:

Integer[] bal = new Integer[friends.size()];
for(int i=0; i < friends.size(); i++){
  bal[i] = friends.get(i);
}

如果您需要,或者您有任何其他問題,請告訴我們!

編輯:如下面的另一個答案中所述,您不必創建新數組並使用ArrayList中的值手動填充它。 您可以使用ArrayList類上的toArray()方法將ArrayList轉換為數組,如下所示: Integer[] bal = friends.toArray(new Integer[friends.size()]);

如果你想簡單地將ArrayList轉換為Array,你可以這樣做: -

Integer[] bal = friends.toArray(new Integer[friends.size()]);

暫無
暫無

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

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