[英]Transferring a 2D array from one class to another
我正在尝试获取用户在文本字段中添加的所有信息,然后将它们添加为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数组?
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());
最简单的方法:只需调用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数组是否很烂,如果您最好创建一个类来保存此信息,然后将该类的对象传递到您的其他实例中,那就更好了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.