简体   繁体   English

Java中的二维字符串数组

[英]Two dimensional string array in java

I am new to java please help me with this issue. 我是Java新手,请帮助我解决这个问题。
I have a string lets say 我有一个字符串可以说

adc|def|efg||hij|lmn|opq

now i split this string and store it in an array using 现在我拆分此字符串并将其存储在数组中

String output[] = stringname.split("||");

now i again need to split that based on '|' 现在我再次需要基于'|'进行拆分 and i need something like 我需要类似的东西

arr[1][]=adc,arr[2][]=def
and so on so that i can access each and every element. 等等,以便我可以访问每个元素。 something like a 2 dimensional string array. 类似于二维字符串数组。 I heard this could be done using Arraylist , but i am not able to figure it out. 我听说可以使用Arraylist完成此操作,但我无法弄清楚。 Please help. 请帮忙。

Here is your solution except names[0][0]="adc", names[0][1]="def" and so on: 这是您的解决方案,除了名称[0] [0] =“ adc”,名称[0] [1] =“ def”等:

String str = "adc|def|efg||hij|lmn|opq";
String[] obj = str.split("\\|\\|");
    int i=0;
    String[][] names = new String[obj.length][]; 

    for(String temp:obj){
        names[i++]=temp.split("\\|");

    }
List<String[]> yourList = Arrays.asList(names);// yourList will be 2D arraylist.
System.out.println(yourList.get(0)[0]); // This will print adc.
System.out.println(yourList.get(0)[1]); // This will print def.
System.out.println(yourList.get(0)[2]); // This will print efg.
               // Similarly you can fetch other elements by yourList.get(1)[index]

What you can do is: 您可以做的是:

String str[]="adc|def|efg||hij|lmn|opq".split("||");
String str2[]=str[0].split("|");
str2 will be containing abc, def , efg
// arrays have toList() method like:

Arrays.asList(any_array);

Can hardly understand your problem... 很难理解你的问题...

I guess you may want to use a 2-dimenison ArrayList : ArrayList<ArrayList<String>> 我猜你可能想使用2维ArrayList: ArrayList<ArrayList<String>>

String input = "adc|def|efg||hij|lmn|opq";
ArrayList<ArrayList<String>> res = new ArrayList<ArrayList<String>>();
for(String strs:input.split("||")){
    ArrayList<String> strList = new ArrayList<String>();
    for(String str:strs.split("|"))
        strList.add(str);
    res.add(strList);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM