Java 在列表内的字符串[]中查找值并删除字符串[] List data=new ArrayList(); 添加(新字符串[]{“bdc”、“house2”、“car2”}); data.add(新字符串[]{“abc”,“house”,“car”});
我必须回答以下问题:Java 在列表内的字符串[]中查找值并删除字符串[] List data=new ArrayList(); 添加(新字符串[]{“bdc”、“house2”、“car2”}); data.add(新字符串[]{“abc”,“house”,“car”});,java,arrays,string,list,Java,Arrays,String,List,我必须回答以下问题: 如何查找例如,什么是值或第二个参数如果我有abc,我想查找值house?(我不知道我的第二个论点是什么,当然只有第一个) 如何删除所有字符串[],如果我再次使用例如abc 要访问数组的给定索引,请使用方括号array[index],知道index从0到array.length-1 要在对集合进行迭代时删除该集合的元素,可以使用迭代器.remove() 因此,您的代码可能如下所示: List<String[]> data = new ArrayList<&g
abc
,我想查找值house
?(我不知道我的第二个论点是什么,当然只有第一个)字符串[]
,如果我再次使用例如abc
array[index]
,知道index
从0
到array.length-1
李>
集合进行迭代时删除该集合的元素,可以使用迭代器.remove()
李>
因此,您的代码可能如下所示:
List<String[]> data = new ArrayList<>();
data.add(new String[]{"bdc", "house2", "car2"});
data.add(new String[]{"abc", "house", "car"});
响应更新:
// Flag used to know if it has already been found
boolean found = false;
for (Iterator<String[]> it = data.iterator(); it.hasNext();) {
String[] values = it.next();
// Check if the first element of the array is "abc"
if (values.length > 1 && "abc".equals(values[0])) {
if (found) {
// Already found so we remove it
it.remove();
continue;
}
// Not found yet so we simply print it
System.out.println(values[1]);
found = true;
}
}
int index = 0;
for (Iterator<String[]> it = data.iterator(); it.hasNext();index++) {
...
if (values.length > 1 && "abc".equals(values[0])) {
System.out.printf("abc found at %d%n", index);
...
}
}
当我们获得匹配项时,您似乎希望在列表中获得索引,您可以简单地添加一个变量index
,该变量也将在for
循环中递增
house
1.遍历列表,找到包含“abc”
的列表,然后查看第二个值是什么。类似于(for(inti=0;i
。要在索引中删除,可以执行List.remove(i);
如果要使用键查找/删除值,则最好选择映射。但“清除”清理所有列表?我想清除或删除索引中的“abc”,例如索引1或任何该“abc”的位置,然后输出如下:house
[abc,house,car]
[bdc,house 2,car2]
第一行是我想要的,但当我再次打印“data”字符串[][abc,house,car]
在那里,所以我需要删除我找到单词的索引,在本例中是“abc”,thanx供您使用数组。toString(myArray)要澄清,当我使用第一个参数找到第二个参数时,在本例中是使用abc
的house
,有没有办法找到列表中的索引?
abc found at 1
house