我正在尝试将显示模式的一维数组拆分为二维字符串数组,尽管我遇到了问题,需要拆分已排序的显示模式数组。我的问题:如何将已经排序的单个数组拆分为二维数组?代码(对不起,奇怪的变量名):
public static String[][] OrganizeDisplayModes (DisplayMode[] modes) {
int iter = 0;
int deltaIter = 0;
int rows = 0;
int columns = 0;
String[][] tobe;
//bubble sorting
for (int a = 0; a < modes.length - 1; a++) {
for(int i = 0; i < modes.length - 1; i++) {
if (modes[i].getWidth() < modes[i+1].getWidth()) {
DisplayMode change = modes[i];
modes[i] = modes[i+1];
modes[i+1] = change;
}
}
}
for(int i = 0; i < modes.length - 1; i++) {
if ((modes[i].getWidth() == modes[i+1].getWidth()) && (modes[i].getBitsPerPixel() < modes[i+1].getBitsPerPixel())) {
DisplayMode change = modes[i];
modes[i] = modes[i+1];
modes[i+1] = change;
}
}
for(int i = 0; i < modes.length - 1; i++) {
if ((modes[i].getWidth() == modes[i+1].getWidth()) && (modes[i].getFrequency() < modes[i+1].getFrequency())) {
DisplayMode change = modes[i];
modes[i] = modes[i+1];
modes[i+1] = change;
}
}
for(int i = 0; i < modes.length; i++) {
DisplayMode current = modes[i];
System.out.println(i + ". " + current.getWidth() + "x" + current.getHeight() + "x" +
current.getBitsPerPixel() + " " + current.getFrequency() + "Hz");
}
//fit into string array
for (int i = 0; i < modes.length - 1; i++) {
if (!(modes[i].getWidth() == modes[i+1].getWidth())) {
rows += 1;
deltaIter = i - deltaIter;
if (deltaIter > columns)
columns = deltaIter;
}
}
//split the displaymode array into the two-dimensional string one here
tobe = new String[rows][columns];
for (int i = 0; i < rows; i++) {
for (int a = 0; a < columns; a++) {
if((modes[iter].getWidth() == modes[iter+1].getWidth())) {
tobe[i][a] = iter + ". " + modes[iter].toString() + " ";
}
else
break;
if (!(iter >= 68))
iter += 1;
}
if (iter >= 68)
break;
}
tobe[rows-1][columns-1] = (iter + 1) + ". " + modes[iter].toString() + " ";
//test to see that it works
for (int i = 0; i < rows; i++) {
for (int a = 0; a < columns; a++) {
if(tobe[i][a] != null)
System.out.print(tobe[i][a]);
else
break;
}
System.out.println("");
}
System.exit(0);
return null;
}
输出如下所示:
0. 1440x900x32 75Hz
1. 1440x900x16 75Hz
2. 1440x900x32 60Hz
3. 1440x900x16 60Hz
具有所有不同的可能分辨率。基本上,我要做的是制作一个不同分辨率的可读列表,以便用户可以选择他们想要的分辨率。谢谢你。