英文:
List of ints input to ArrayList Java
问题
我有一些需要添加到ArrayList的值,因为它们需要彼此连接,同时又需要能够被移除。我脑海中构想的装置有点类似海战游戏。我有一些需要点击的方框。当你点击其中一个时,方框会变成绿色。当你点击完所有配对的方框时,它们会变成金色方框。
我的问题是:是否有一种更流畅和简便的方法将下面的值添加到ArrayList?我尝试了一些for循环,但都没有成功;
ArrayList<Integer>[][] Boxes = new ArrayList[6][2];
Boxes[0][0].add(1110);
Boxes[0][1].add(1210);
Boxes[0][2].add(1310);
Boxes[0][3].add(1410);
Boxes[1][0].add(2110);
Boxes[1][1].add(2210);
Boxes[1][2].add(2310);
Boxes[2][0].add(2121);
Boxes[2][1].add(2221);
Boxes[2][2].add(2321);
Boxes[3][0].add(3110);
Boxes[3][1].add(3210);
Boxes[4][0].add(3120);
Boxes[4][1].add(3220);
Boxes[5][0].add(3130);
Boxes[5][1].add(3230);
英文:
I've values that need to be added into a ArrayListbecause they have to be connected to each other and at the same time needs to be removeable. the contraption in my head is somewhat similar to naval battle. I've boxes that needs to be clicked. when you click one of them right the box turns green. when you clicked all of the pared boxes they turn into golden boxes.
my question is: is there a more smooth and easy going wat to add the values below to a ArrayList? I've tried some forloops but all didnt work;
ArrayList[][] Boxes = new ArrayList[6][];
Boxes[0][0].add(1110);
Boxes[0][1].add(1210);
Boxes[0][2].add(1310);
Boxes[0][3].add(1410);
Boxes[1][0].add(2110);
Boxes[1][1].add(2210);
Boxes[1][2].add(2310);
Boxes[2][0].add(2121);
Boxes[2][1].add(2221);
Boxes[2][2].add(2321);
Boxes[3][0].add(3110);
Boxes[3][1].add(3210);
Boxes[4][0].add(3120);
Boxes[4][1].add(3220);
Boxes[5][0].add(3130);
Boxes[5][1].add(3230);
</details>
# 答案1
**得分**: 0
以下是翻译好的内容:
```java
// 提示有关原始类型的警告
private static ArrayList[] buildArrayOfLists(Integer... arr) {
return Arrays.stream(arr)
.map(i -> new ArrayList<>(Arrays.asList(i)))
.toArray(ArrayList[]::new);
}
// 使用示例 -- 提示有关原始类型的警告
ArrayList[][] boxes = new ArrayList[][] {
buildArrayOfLists(1110, 1210, 1310, 1410),
buildArrayOfLists(2110, 2210, 2310),
buildArrayOfLists(2121, 2221, 2321),
buildArrayOfLists(3110, 3210),
buildArrayOfLists(3120, 3220),
buildArrayOfLists(3130, 3230)
};
Arrays.stream(boxes)
.map(Arrays::toString)
.forEach(System.out::println);
输出:
[[1110], [1210], [1310], [1410]]
[[2110], [2210], [2310]]
[[2121], [2221], [2321]]
[[3110], [3210]]
[[3120], [3220]]
[[3130], [3230]]
**更新**<br/>
为了消除关于在`ArrayList[][]`中使用原始类型的警告,并且使用一般的`List`代替特定的实现,可以对方法及其用法进行重构,使其更加类型安全且无警告:
```java
private static <T> List<?>[] arrayOfLists(T... arr) {
return Arrays.stream(arr)
.map(i -> new ArrayList<>(Arrays.asList(i)))
.toArray(List<?>[]::new);
}
// 使用示例 - 没有关于原始类型的警告,显式抑制未经检查的警告
@SuppressWarnings("unchecked")
List<Integer>[][] safeBoxes = (List<Integer>[][]) new List<?>[][] {
arrayOfLists(1110, 1210, 1310, 1410),
arrayOfLists(2110, 2210, 2310),
arrayOfLists(2121, 2221, 2321),
arrayOfLists(3110, 3210),
arrayOfLists(3120, 3220),
arrayOfLists(3130, 3230)
};
英文:
If you provide a helper method to build array of lists, the initialization may be compacted:
// warning about rawtypes
private static ArrayList[] buildArrayOfLists(Integer... arr) {
return Arrays.stream(arr)
.map(i -> new ArrayList<>(Arrays.asList(i)))
.toArray(ArrayList[]::new);
}
// usage -- warning about rawtypes
ArrayList[][] boxes = new ArrayList[][] {
buildArrayOfLists(1110, 1210, 1310, 1410),
buildArrayOfLists(2110, 2210, 2310),
buildArrayOfLists(2121, 2221, 2321),
buildArrayOfLists(3110, 3210),
buildArrayOfLists(3120, 3220),
buildArrayOfLists(3130, 3230)
};
Arrays.stream(boxes)
.map(Arrays::toString)
.forEach(System.out::println);
Output:
[[1110], [1210], [1310], [1410]]
[[2110], [2210], [2310]]
[[2121], [2221], [2321]]
[[3110], [3210]]
[[3120], [3220]]
[[3130], [3230]]
Update<br/>
In order to get rid of warnings about using raw types for ArrayList[][]
and use general List
instead of specific implementation, it is possible to refactor the method and its usage in a more typesafe and warning-free way:
private static <T> List<?>[] arrayOfLists(T... arr) {
return Arrays.stream(arr)
.map(i -> new ArrayList<>(Arrays.asList(i)))
.toArray(List<?>[]::new);
}
// usage - no warning about rawtypes, explicitly suppress unchecked
@SuppressWarnings("unchecked")
List<Integer>[][] safeBoxes = (List<Integer>[][]) new List<?>[][] {
arrayOfLists(1110, 1210, 1310, 1410),
arrayOfLists(2110, 2210, 2310),
arrayOfLists(2121, 2221, 2321),
arrayOfLists(3110, 3210),
arrayOfLists(3120, 3220),
arrayOfLists(3130, 3230)
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论