二维字符串数组在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
改编[1] [] = ADC,编曲[2] [] =高清
结果等等,这样我可以访问每一个元素。
就像一个二维字符串数组。
听说这个可以用的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.
推荐答案
下面是除了名的解决方案[0] [0] =ADC,名字[0] [1] =高清等等:
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屋!