简体   繁体   中英

Transferring a 2D array from one class to another

I am attempting to take all the information that added by the user in the text fields, and then add them a 2D array. I have accomplish that by doing this:

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());

But I can't figure out how to transfer the array to a different class, where it will be used in an object constructor that will allow me to print via the objects toString method.

How should I be going about transferring the 2D array from class to class?

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.

The simplest way: simply call a setter method, and pass the array into the instance:

    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);

Where you get your reference to the otherInstance instance will depend totally on how your program is structured, information that we are not privy to at this moment.

Having said this, I do have to wonder if your 2D array is a kludge, if you're far better off creating a class to hold this information, and then passing an object of this class into your other instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM