java中的二维字符串数组 [英] Two dimensional string array in java
问题描述
我是 Java 新手,请帮我解决这个问题.
我有一个字符串可以说
I am new to java please help me with this issue.
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
等等,这样我就可以访问每个元素.类似于二维字符串数组.我听说这可以使用 Arraylist 来完成,但我无法弄清楚.请帮忙.
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.
Please help.
推荐答案
这里是你的解决方案,除了 names[0][0]="adc", names[0][1]="def" 等等:
Here is your solution except names[0][0]="adc", names[0][1]="def" and so on:
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]
这篇关于java中的二维字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!