如何将单个数组组织成二维数组?

时间:2013-05-18 23:19:21

标签: java algorithm lwjgl

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

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

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

1 个答案:

答案 0 :(得分: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()的覆盖进行排序