[英]How to convert a 2D ArrayList to a 2D String?
我正在尝试将2D ArrayList“ dataArrayList”转换为2D String数组,以便可以按字母顺序对其进行排序,但是在尝试时会发出警告,说:
Suspicious Collection.toArray() call. Collection item type java.util.List<java.lang.String>
is not assignable to array component type java.lang.String[]
当我尝试运行它时,它失败了。 这是我的代码:
public static List<List<String>> dataArrayList = new ArrayList<>();
public static void main(String args[]) {
try {
readFile();
alphaSort();
} catch (Exception e) {
}
}
public static void readFile() {
FileReader fileRead = new FileReader(txtFileDirectory);
BufferedReader dataReader = new BufferedReader(fileRead);
String line = "";
for (int i = 0; line != null; i ++) {
line = dataReader.readLine();
if (line != null)
dataArrayList.add(Arrays.asList(line.split(",");
}
}
public static void alphaSort() {
String[][] alphaArray = new String[dataArrayList.Size()][15]
//warning underline on "alphaArray"
dataArrayList.toArray(alphaArray);
}
编辑:看起来这已经完成: 将ArrayList转换为包含数组长度不同的2D数组 。
您遇到错误,因为您需要将List
两个级别都转换为array
。 尝试遍历列表并将每个子列表转换为数组。
public static void alphaSort() {
String[][] alphaArray = new String[dataArrayList.Size()][15];
for(int i = 0; i < dataArrayList.size(); i++)
alphaArray[i] = dataArrayList.get(i).toArray(new String[15]);
}
上面链接的问题的第二个答案为我上面介绍的方法提供了一种替代方法:
public static void alphaSort() {
ArrayList<String[]> tempList = new ArrayList<String[]>();
for (ArrayList<String> stringList : dataArrayList)
tempList.add(stringList.toArray(new String[stringList.size()]));
String[][] alphaArray = tempList.toArray(new String[dataArrayList.size()][]);
}
要将列表转换为二维数组,您必须遍历列表内部并将每个元素分配给数组。
我在这里为你写一个变压器。
public static String[][] transformListToArray(List<List<String>> inputList) {
int rows = inputList.size();
int columns = inputList.get(0).size();
String[][] array2d = new String[rows][columns];
for( int i = 0; i < inputList.size(); i++ )
for( int j = 0; j < inputList.get(i).size(); j++ )
array2d[i][j] = inputList.get(i).get(j);
return array2d;
}
打印也很相似,只需要您遍历数组内部并格式化输出即可。
我也写了这个用于打印二维数组。
public static void print2DArray( String[][] inputArray) {
for( int i = 0; i < inputArray.length; i++ ) {
for( int j = 0; j < inputArray[i].length; j++ )
System.out.printf("%s ", inputArray[i][j]);
System.out.println();
}
}
作为一个完整的解决方案,您可以检查一下,它工作正常;
public class TestConvert2DArray {
public static List<List<String>> dataArrayList = new ArrayList<>();
public static void main(String args[]) {
try {
readFile();
// alphaSort(); we dont need it anymore
String new2dArray[][] = transformListToArray(dataArrayList);
print2DArray(new2dArray);
} catch (Exception e) {
}
}
// To be used on transforming the list to 2d-array
public static String[][] transformListToArray(List<List<String>> inputList) {
int rows = inputList.size();
int columns = inputList.get(0).size();
String[][] array2d = new String[rows][columns];
for (int i = 0; i < inputList.size(); i++)
for (int j = 0; j < inputList.get(i).size(); j++)
array2d[i][j] = inputList.get(i).get(j);
return array2d;
}
// To be used on printing the 2d-array to the console
public static void print2DArray(String[][] inputArray) {
for (int i = 0; i < inputArray.length; i++) {
for (int j = 0; j < inputArray[i].length; j++)
System.out.printf("%s ", inputArray[i][j]);
System.out.println();
}
}
// Just added an init for textFileDirectory
public static void readFile() throws Exception {
String txtFileDirectory = "D:\\try.txt"; // "D:\\try.txt"
FileReader fileRead = new FileReader(txtFileDirectory);
BufferedReader dataReader = new BufferedReader(fileRead);
String line = "";
// And you dont need and index inside the for loop
for (; line != null;) {
line = dataReader.readLine();
if (line != null)
dataArrayList.add(Arrays.asList(line.split(",")));
}
dataReader.close(); // You have to close all the reasources to prevent
// data leak
}
}
样本输入与输入文件相同。
01 - What comes around, goes around
02 - Eyes wide open
03 - What comes around, goes around
04 - Eyes wide open
05 - What comes around, goes around
06 - Eyes wide open
07 - What comes around, goes around
08 - Eyes wide open
09 - What comes around, goes around
10 - Eyes wide open
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.