[英]How to get an element from an ArrayList<ArrayList<String[]>?
[英]How to get an element from a String Array, if the String array itself is an element of an ArrayList?
讓我給你舉個例子;
String[] one = {"one", "two"};
String[] two = {"bob", "lol", "hi"};
List<String[]> list = new ArrayList<String[]>();
list.add(one);
list.add(two);
現在,我想在列表中獲取第二個字符串數組(即“兩個”)。 我這樣做是:
list.get(2);
但是,假設我想獲取List中兩個String數組中的第二個元素(基本上我想從list-> two-> lol中獲取字符串“ lol”)。
這是你怎么做的:
list.get(2).get(2)
Java(以及大多數編程語言)中的索引以0
開頭 ,因此,如果要訪問第二個元素,則必須使用索引1
:
list.get(0)[1];
注意
list.get(0)
將返回第一個 String[]
數組,並且必須使用以下語法來訪問數組的元素:
someArray[index]
數組從索引0開始。因此您必須使用list.get(i)[1]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.