将2D数组从一个类传输到另一个类

时间:2015-11-29 03:49:59

标签: java class multidimensional-array transfer

我试图在文本字段中获取用户添加的所有信息,然后将它们添加为2D数组。我通过这样做完成了这个:

int minorinput = JOptionPane.showConfirmDialog(frame, panel1, "Choose Minor Stats", JOptionPane.OK_CANCEL_OPTION);

        if(minorinput == JOptionPane.OK_OPTION)
        {
            int [][] pureMR = new int [4][4];

            pureMR[0][0] = Integer.parseInt(Melee.getText());
            pureMR[1][0] = Integer.parseInt(Ranged.getText());
            pureMR[2][0] = Integer.parseInt(RC.getText());
            pureMR[3][0] = Integer.parseInt(Negotiation.getText());

            pureMR[0][1] = Integer.parseInt(Dodge.getText());
            pureMR[1][1]= Integer.parseInt(Perception.getText());
            pureMR[2][1] = Integer.parseInt(Will.getText());
            pureMR[3][1] = Integer.parseInt(Procure.getText());

            pureMR[0][2] = Integer.parseInt(rideBox.getText());
            pureMR[1][2] = Integer.parseInt(rideBox2.getText());
            pureMR[2][2] = Integer.parseInt(artBox.getText());
            pureMR[3][2] = Integer.parseInt(art2.getText());

            pureMR[0][3] = Integer.parseInt(knowledgeBox.getText());
            pureMR[1][3] = Integer.parseInt(knowledge2Box.getText());
            pureMR[2][3] = Integer.parseInt(infoBox.getText());
            pureMR[3][3] = Integer.parseInt(info2Box.getText());

但是我无法弄清楚如何将数组转移到另一个类,它将在一个对象构造函数中使用,它允许我通过对象toString方法进行打印。

我应该如何将2D数组从类转移到类?

Class I want to add the Array to:

                    if(pureinput == JOptionPane.OK_OPTION)
                    {

                        String name = nameBox.getText();
                        String age = ageBox.getText();
                        String gender = genderBox.getText();
                        String bloodType = bloodBox.getText();
                        String height = heightBox.getText();
                        String weight = weightBox.getText();
                        String zodiac= zodiacBox.getText();
                        String work = workBox.getText();
                        String cover = coverBox.getText();
                        String breed = "Pure";
                        String sydrome = syndrome1Box.getText();

                        int[] pureBS = new int[4];
                        pureBS[0] = Integer.parseInt(bodyBox.getText());
                        pureBS[1] = Integer.parseInt(senseBox.getText());
                        pureBS[2] = Integer.parseInt(mindBox.getText());
                        pureBS[3] = Integer.parseInt(skillBox.getText());

                        int[] pureSS = new int[6];
                        pureSS[0] = Integer.parseInt(mHPBox.getText());
                        pureSS[1] = Integer.parseInt(stockBox.getText());
                        pureSS[2] = Integer.parseInt(savingsBox.getText());
                        pureSS[3] = Integer.parseInt(initBox.getText());
                        pureSS[4] = Integer.parseInt(moveBox.getText());
                        pureSS[5] = Integer.parseInt(dashBox.getText());

                        String origin = orginBox.getText();
                        String exp = ExperienceBox.getText();
                        String encounter = EncounterBox.getText();
                        String awake = AwakeningBox.getText();
                        int eRate = Integer.parseInt(EncroachmentRateBox.getText());
                        String impulse = ImpulseBox.getText();
                        int eRate2= Integer.parseInt(EncroachmentRateBox2.getText());


                        Character pure = new Character(name,age,bloodType,gender, height,weight,zodiac,work,cover,
                                                        breed,sydrome,pureBS, pureSS,origin,exp,encounter,awake,eRate,
                                                        impulse,eRate2, //Needs a 2D array here);

                        System.out.println(pure.pureToString());

It creates this dialog box.

1 个答案:

答案 0 :(得分:0)

最简单的方法:只需调用setter方法,并将数组传递给实例:

    if(minorinput == JOptionPane.OK_OPTION) {
        int [][] pureMR = new int [4][4];

        // code to fill array

        // assuming your OtherClass has this type of setter method...
        // because if it doesn't, it NEEDS one.
        otherInstance.setPureMr(pureMR);

如果您对otherInstance实例的引用将完全取决于您的程序的结构,此时我们不知道的信息。

话虽如此,我不得不怀疑你的2D数组是否是一个kludge,如果你在创建一个类来保存这些信息,然后将这个类的对象传递给你的另一个实例要好得多。