英文:
Indexing in Multidimensional ArrayList in Java
问题
我对ArrayList还不熟悉,我在这里遇到了问题。有人能帮忙吗?
英文:
I am new to ArrayList, i am kind of stuck here. can anyone please help.
ArrayList Arr = new ArrayList();
Arr = [['3454' , '546',[5,6,8,9]],['abc',7,'efg']]
I have input the values in an Arraylist and I want to know the size of an arraylist [5,6,8,9] which is inside the 0th element of main arraylist.
I tried Arr.get(0).get(2).size();
But this shows error in my code.
Thanks in advance.
答案1
得分: 2
@Test
public void testSomething() {
List<Object> innerList1 = new ArrayList<>();
List<Object> innerList2 = new ArrayList<>();
innerList1.add("3454");
innerList1.add("546");
innerList1.add(Arrays.asList(new int[]{5, 6, 8, 9}));
innerList2.add("abc");
innerList2.add(7);
innerList2.add("efg");
List<List<Object>> list = new ArrayList<>();
list.add(innerList1);
list.add(innerList2);
System.out.println(list.get(0).get(0));
System.out.println(list.get(0).get(1));
System.out.println(list.get(0).get(2));
System.out.println(list.get(1).get(0));
System.out.println(list.get(1).get(1));
System.out.println(list.get(1).get(2));
}
这是您可以实现它的方式。
长回答:
您不应该以这种方式处理,因为您每次都需要检查所在位置的类型。可以打印这些值,但您无法将其求和。
对我来说,感觉您刚开始学习Java,之前可能花了一些时间学习JavaScript,这可能是您提出这样问题的原因。
因此,我建议您从Java基础开始学习一些课程。
英文:
Short answer:
@Test
public void testSomething() {
List<Object> innerList1 = new ArrayList<>();
List<Object> innerList2 = new ArrayList<>();
innerList1.add("3454");
innerList1.add("546");
innerList1.add(Arrays.asList(new int[]{5, 6, 8, 9}));
innerList2.add("abc");
innerList2.add(7);
innerList2.add("efg");
List<List<Object>> list = new ArrayList<>();
list.add(innerList1);
list.add(innerList2);
System.out.println(list.get(0).get(0));
System.out.println(list.get(0).get(1));
System.out.println(list.get(0).get(2));
System.out.println(list.get(1).get(0));
System.out.println(list.get(1).get(1));
System.out.println(list.get(1).get(2));
}
That's how you can do it.
Long answer:
You shouldn't do it that way, because you'll need to check each time for the type that you have there. It's ok to print these values but you won't be able to sum it up.
It feels for me that you just started with Java after spending some time with js and that's why you ask a question like that.
So my advice for you would be to start some course from basics of java.
答案2
得分: 0
为了分解这个问题:
1. 要输入这些值,你可以以几种不同的方式创建嵌套的 ArrayList。对于较短的内容,你可以使用以下方法。
```java
ArrayList arr = new ArrayList(
Arrays.asList(new Object[]{
"3454",
"546",
new ArrayList(
Arrays.asList(5, 6, 8, 9)),
new ArrayList(
Arrays.asList(new Object[]{
"abc", 7, "efg"
}))
}));
- 要获取 [5,6,8,9] 元素的大小,你可以这样做。由于在主列表中混合使用了不同的类型,你需要将其转换为 ArrayList。
arr.get(2) -> 这将返回 ArrayList(Arrays.asList(5, 6, 8, 9))
(ArrayList) arr.get(2) -> 这将将其转换为 ArrayList,以便您可以在其上调用 ArrayList 的方法
将所有内容放在一起:
int size = ((ArrayList) arr.get(2)).size();
System.out.println("Size: " + size);
>>>Size: 4
<details>
<summary>英文:</summary>
To breakdown the question:
1. To input the values you can create the nested ArrayLists in several different ways. For something short, you can use this approach.
ArrayList arr = new ArrayList(
Arrays.asList(new Object[]{
"3454",
"546",
new ArrayList(
Arrays.asList(5, 6, 8, 9)),
new ArrayList(
Arrays.asList(new Object[]{
"abc", 7, "efg"
}))
}));
2. To get the size of the [5,6,8,9] element you can do it this way. You need to do the cast to ArrayList as you mix different types in the main list.
arr.get(2) -> this will return ArrayList(Arrays.asList(5, 6, 8, 9))
(ArrayList) arr.get(2) -> this will cast it to ArrayList so you can call methods from ArrayList on it
Whole thing together:
int size = ((ArrayList) arr.get(2)).size();
System.out.println("Size: " + size);
>>>Size: 4
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论