我在java中使用arraylist。 我写了以下代码:
public void myfunction() {
cmdMessage="str1 : root;str2 : cisco123,12345,2345;str1 : root234;str2 : cccccc";
String[] splitMessage = cmdMessage.split(";");
System.out.println("string length is \n" + splitMessage.length);
ArrayList<String> mylist = new ArrayList<>();
ArrayList<ArrayList> finalresult = new ArrayList<>();
for (int i= 0; i<= splitMessage.length-1;i++){
if(splitMessage[i]!=null){
if (splitMessage[i].contains("str1")){
System.out.println("splitted string is "+ (splitMessage[i]).split(":")[1].trim());
mylist.add(((splitMessage[i]).split(":")[1]).trim());
}
if (splitMessage[i].contains("str2")){
System.out.println("str2 length is ..." + splitMessage[i].length());
System.out.println("splitted str2 string is "+ (splitMessage[i]).split(":")[1].trim());
mylist.add(((splitMessage[i]).split(":")[1]).trim());
}
}
finalresult.add(mylist);
}
}
这个Java函数将结果表示为:
[[root, cisco123,12345,2345, root234, cccccc],[root, cisco123,12345,2345, root234, cccccc]]
但我希望结果如下:
[[root, cisco123,12345,2345], [root234, cccccc]]
我想我在某个地方缺少declaration/initialization
技巧。
我如何得到这个结果?
答案 0 :(得分:2)
尝试将mylist
的初始化移动到for循环中。
答案 1 :(得分:1)
试试这个:
public static void main(String... args) {
String cmdMessage="str1 : root;str2 : cisco123,12345,2345;str1 :
root234;str2 : cccccc";
List myList = new ArrayList();
String[] str = cmdMessage.split(";");
List<List> finalList= new ArrayList();
for(String s: str)
{
if(s.contains("str1"))
{ String[] newStr=s.split(":");
myList.add( newStr[1].trim());
}
if(s.contains("str2"))
{ String[] newStr=s.split(":");
myList.add( newStr[1].trim());
finalList.add(myList);
myList=new ArrayList();
}
}
Iterator it = finalList.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
OP:
[root, cisco123,12345,2345]
[root234, cccccc]
finalList : [[root, cisco123,12345,2345], [root234, cccccc]]
答案 2 :(得分:0)
这里发生的是无论正在执行哪个for循环迭代,元素都被添加到同一个列表mylist
。在每次迭代结束时,“mylist”将添加到finalresult
。也就是说,在这种情况下,相同列表的两个副本将添加到finalresult
。
要解决此问题,您必须在for循环的每次迭代开始时创建新列表,并将元素添加到该列表中。在每次迭代结束时,将新创建的列表添加到finalresult
。
for (int i= 0; i<= splitMessage.length-1;i++){
ArrayList<String> mylist = new ArrayList<>();
// do something with strings...
// ...
finalresult.add(mylist);
}
答案 3 :(得分:0)
for (int i= 0; i<= splitMessage.length-1;i++){
if(splitMessage[i]!=null){
if (splitMessage[i].contains("str1")){
System.out.println("splitted string is "+ (splitMessage[i]).split(":")[1].trim());
mylist.add(((splitMessage[i]).split(":")[1]).trim());
}
if (splitMessage[i].contains("str2")){
System.out.println("str2 length is ..." + splitMessage[i].length());
System.out.println("splitted str2 string is "+ (splitMessage[i]).split(":")[1].trim());
mylist.add(((splitMessage[i]).split(":")[1]).trim());
finalresult.add(mylist); // adding after str2
mylist = new ArrayList<>();// creating new list after str
}
}
}
答案 4 :(得分:0)
你可以使用这样的正则表达式:
public static void myfunction() {
ArrayList<String> mylist;
ArrayList<ArrayList> finalresult = new ArrayList<>();
String cmdMessage = "str1 : root; str2 : cisco123,12345,2345; str1 : root234; str2 : cccccc";
String trimmedMessage = cmdMessage.replaceAll(" ", "");
String[] splitMessage = trimmedMessage.split("str[0-9]:");
for(int i =0;i<splitMessage.length;i++){
if(splitMessage[i].length()>0 && splitMessage[i].contains("root")){
System.out.println("splitted string is " +splitMessage[i] + " " +splitMessage[i+1]);
mylist = new ArrayList<>();
mylist.add(splitMessage[i].replaceAll("[;,:]",""));
mylist.add(splitMessage[i+1].replaceAll("[;:]",""));
i +=1;
System.out.println("Adding list "+Arrays.asList(mylist));
finalresult.add(mylist);
}
}
System.out.println(Arrays.asList(finalresult));
}