交错的二维数组?

时间:2014-12-16 07:55:19

标签: java arrays json tree

所以我有一堆id号,它们有点像树......例如

1               3082                          2986
              /      \                      /      \
2         1233        2344              1233        1634
         /    \      /    \                        /    \
3      0254  0456  1342  0653                    0643  0463

我如何制作一个数组数组以适应这个方案,还是我还能做些什么呢?我知道我可以创建一个二维数组(String [] [] array = new ...)但是每棵树的大小总是不同且未知。有些可能是1x1,有些可能是3x5,最大。我真的不想制作一个特别大的阵列,至少我不认为我这样做。

我正在使用java,顺便说一下,所有内容都以JSONArray开头。

更新 所以我添加了id号码的例子,但它有点无关紧要。因此,如果我合并0643和0463,我将得到1634,那么如果我将1643与1233合并,我将得到我的最终项目2986.再次,它也无关紧要如何工作。

我只是想知道如果数组的大小未知,是否可以制作一个二维数组,因为你可以看到第1列只包含1个id号,第2列将包含2个id号情况1或3个id号,第3列可以包含1到6个id号。

1 个答案:

答案 0 :(得分:0)

我为你制作了一个演示程序,看看如何用一组未知的列和行来实现二维数组 - 我使用ArrayList类首先存储一组未知的项,然后将其添加到二维阵列。请注意我在创建一个参差不齐的二维数组时使用的语法:(注意:一个参差不齐的二维数组是每行中列数不同的数组)

    import javax.swing.JOptionPane;
import java.util.ArrayList;
public class ss {
    public static void main(String[] args){
        JOptionPane.showMessageDialog(null, "Welcome to woodrow's program.\n" +
            "Here, you create a 2d array as big as you want..  2D array demo.");

        ArrayList<String> names = new ArrayList<>();
            while(true) {
                String nameInput = JOptionPane.showInputDialog("Enter names here:  Hit cancel when done.");
                if(nameInput == null || nameInput.equals(""))
                    break;
                names.add(nameInput);
            }
        String[][] pplInfo = new String[names.size()][];

        for(int i = 0, accumulator = 0; i < names.size(); i++) {
            ArrayList<String> pets = new ArrayList<>();
            int innerAccumulator = -1;
                    String petInput = "";
                    do{
                        petInput = JOptionPane.showInputDialog("Enter the names of " + names.get(accumulator) + 
                                "'s pets here:  (hit cancel when done)");
                        innerAccumulator++;
                        if(petInput != null || petInput.equals("")) { 
                            pets.add(petInput); 
                        }
                    }while(petInput != null && !petInput.equals(""));

                    accumulator++;

            pplInfo[i] = new String[innerAccumulator];
            //the row of person i, is going to have innerAccumulator columns listing their pets.
            //now add the pets to the appropriate row
            for(int ii = 0; ii < innerAccumulator; ii++)
                pplInfo[i][ii] = pets.get(ii);
        }
        //now to display the data you have entered in the ragged array.
        //ragged means that the number of columns differs for each row.
        for(int i = 0; i < pplInfo.length; i++) {//pplInfo.length returns number of rows
            System.out.println(names.get(i) + "'s pet list:");
            for(int ii = 0; ii < pplInfo[i].length; ii++) //pplInfo[rowIndex].length returns  # columns in respective row
                System.out.println((ii+1) + ". " + pplInfo[i][ii]);
            System.out.println("======================");
        }
        System.exit(0);
    }
}

我从:

开始

String [] [] str = new String [rows] [];

我用int指定的第一个框,即数组中的行数。我现在把第二个支架留空了;稍后,我通过执行以下操作指定每行中的列数:

str [index] = new String [columns];

现在,我拥有了我想要的二维数组,并且可以在嵌套的for循环中有效地添加项目。