0

我正在尝试将显示模式的一维数组拆分为二维字符串数组,尽管我遇到了问题,需要拆分已排序的显示模式数组。我的问题:如何将已经排序的单个数组拆分为二维数组?代码(对不起,奇怪的变量名):

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

具有所有不同的可能分辨率。基本上,我要做的是制作一个不同分辨率的可读列表,以便用户可以选择他们想要的分辨率。谢谢你。

4

1 回答 1

0

如何以不同的方式存储信息?当我从 C 过渡到 Java 时,我经常使用多维数组,但这很痛苦。

public class ScreenType implements Comparable {
    int width;
    int height;
    int color; // not sure what this one was for, assuming 32/16 bit color
    int hertz;

    public ScreenType(int w, int h, int c, int h) {
        width = w;
        height = h;
        color = t;
        hertz = h;
    }

    // remember to maintain transitive property,
    // see JDK documentation for Comparable
    @Override
    public int compareTo(Tuple other) {
    }

    // getters and setters and whatnot

    @Override
    public String toString() {
        return width + "X" + height + "X" + color+ "    " + hertz + "hz";
    }
}

然后像这样存储您的屏幕数据并使用 ScreenTypes 的 ArrayList,如果我没记错的话,它将根据您对 compareTo() 的覆盖进行排序

于 2013-05-19T00:03:35.150 回答