Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
int の配列を ArrayList に追加 (実際には追加) するショートカットはありますか? 次の例では
ArrayList<Integer> list=new ArrayList<Integer>(); int[] ints={2,4,5,67,8};
または、リストに ints の要素を 1 つずつ追加する必要がありますか?
他の人が提案したように使用Arrays.asList(ints)しても機能しません(のリストではint[]なくのリストが表示されますInteger)。
Arrays.asList(ints)
int[]
Integer
私が考えることができる唯一の方法は、要素を1つずつ追加することです:
for (int val : ints) { list.add(val); }
int[]に変換できる場合はInteger[]、次を使用できますaddAll()。
Integer[]
addAll()
list.addAll(Arrays.asList(ints));