List of ints input to ArrayList Java 将整数列表输入到ArrayList中 Java

huangapple go评论66阅读模式
英文:

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 -&gt; new ArrayList&lt;&gt;(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 &lt;T&gt; List&lt;?&gt;[] arrayOfLists(T... arr) {
	return Arrays.stream(arr)
                 .map(i -&gt; new ArrayList&lt;&gt;(Arrays.asList(i)))
                 .toArray(List&lt;?&gt;[]::new);
}

// usage - no warning about rawtypes, explicitly suppress unchecked
@SuppressWarnings(&quot;unchecked&quot;)
List&lt;Integer&gt;[][] safeBoxes = (List&lt;Integer&gt;[][]) new List&lt;?&gt;[][] {
      arrayOfLists(1110, 1210, 1310, 1410),
      arrayOfLists(2110, 2210, 2310),
      arrayOfLists(2121, 2221, 2321),
      arrayOfLists(3110, 3210),
      arrayOfLists(3120, 3220),
      arrayOfLists(3130, 3230)
};

huangapple
  • 本文由 发表于 2020年10月22日 02:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64469975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定