I have the following 2d array:
[[Reflux Symptom Sensitivity Index (Impedance)], [Symptom Acid Nonacid All Reflux], [Regurgitate 34% 71% 36%], [Stomach pain 21% 14% 19%], [Chest pain 3% 0% 3%], [], [Reflux Symptom Association Probability (Impedance)]]
I would like to all the inner arrays apart from the first one, by whitespace so that I end up with:
[[Reflux Symptom Sensitivity Index (Impedance)], [Symptom,Acid, Nonacid, All, Reflux], [Symptom,Acid, Nonacid, All, Reflux], [Symptom,Acid, Nonacid, All, Reflux],[Chest pain,3%,0%,3%]]
I could write the following code:
ArrayList<List<String>> Arr_RSI_table2d = new ArrayList<List<String>>();
List<String> stats = Arr_RSI_table2d.get(1);
// remove() returns the object that was removed
String allStats = stats.remove(0);
String allStats2=allStats.trim();
Collections.addAll(stats, allStats2.split("\\s"));
List<String> Arr_RSI_table2d_col2 = Arr_RSI_table2d.get(2);
// remove() returns the object that was removed
String All_Arr_RSI_table2d_col2 = Arr_RSI_table2d_col2.remove(0);
String All_Arr_RSI_table2d_col2_2=allStats.trim();
Collections.addAll(Arr_RSI_table2d_col2, All_Arr_RSI_table2d_col2_2.split("\\s"));
List<String> Arr_RSI_table2d_col3 = Arr_RSI_table2d.get(3);
// remove() returns the object that was removed
String All_Arr_RSI_table2d_col3 = Arr_RSI_table2d_col3.remove(0);
String All_Arr_RSI_table2d_col3_2=allStats.trim();
Collections.addAll(Arr_RSI_table2d_col3, All_Arr_RSI_table2d_col3_2.split("\\s"));
List<String> Arr_RSI_table2d_col4 = Arr_RSI_table2d.get(4);
// remove() returns the object that was removed
String All_Arr_RSI_table2d_col4 = Arr_RSI_table2d_col4.remove(0);
String All_Arr_RSI_table2d_col4_2=allStats.trim();
Collections.addAll(Arr_RSI_table2d_col4, All_Arr_RSI_table2d_col4_2.split("\\s"));
How can I do this?
You could try something like this;
for (int i = 1; i < myArray.size(); i++) {
String[] innerArray = myArray.get(i);
for (int j = 0; j < innerArray.length; j++) {
innerArray[j] = removeWhiteSpace(innerArray[j]);
}
}
You will need a helper method to remove white space:
private String removeWhiteSpace(String toTrim) {
String toRet = "";
String trimSplit[] = toTrim.split(" ");
for (String word : trimSplit) {
if (!word.trim().isEmpty()) {
toRet = toRet + word + " ";
}
}
return toRet.trim();
}
This is untested but it should work fine. Let me know how it goes.
ArrayList<List<String>> myList = new ArrayList();
void loopThroghArray() {
for (List<String> list : myList) {
//outer loop
for (String string : list) {
//inner loop
}
}
}
its hard to understand your code , since you have not given even the type of your array bt here i have made a example on looping through 2dimensional array. im using a enhanced for loop to loop the first array and another enhanced for loop to iterate the 2nd array you can do the spliting inside the 2nd for loop . you might wanna try this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.