将数组数据从一个类发送到另一个类JAVA

时间:2018-05-15 14:24:26

标签: java arrays

(我是初学者所以这可能听起来很明显/缺乏信息。)我有一个不同宠物属性的ArrayList,包括他们的名字,俗名,动物价格,性别,日期等属性买了,卖了。此信息是从单独的类生成的,该类将信息数组添加到已存在的动物列表的数组数组中。本质上,我想将数组发送到另一个类(称为Pets),然后可以将其添加到数组数组中。我知道这可能听起来令人困惑,但这是我能说出来的唯一方法,如果需要,我可以澄清任何事情。任何帮助都会很棒,因为我真的卡住了,无法解决如何发送它。这是在数组中生成我的值的代码(使用文本框输入信息)。

public void actionPerformed(ActionEvent e) {
            ArrayList<String> NewanimalArr = new ArrayList<>();

            String givenName = txtGivenname.getText();
            String commonName = txtCommonName.getText();
            String priceOf = txtPrice_1.getText();
            String sexOf = txtSex.getText();
            String colourOf = txtMaincolour.getText();
            String dateOfA = txtArrivaldate.getText();
            String dateSold = txtSellingdate.getText();

            NewanimalArr.add(givenName);
            NewanimalArr.add(commonName);
            NewanimalArr.add(priceOf);
            NewanimalArr.add(sexOf);
            NewanimalArr.add(colourOf);
            NewanimalArr.add(dateOfA);
            NewanimalArr.add(dateSold);

            System.out.println(NewanimalArr);
        }
    });

然后打印生成的信息,例如: [alex,Dog,40.50,Male,Brown,14/04 / 2015,14 / 12/2016] 然后我如何将此数据发送到另一个类

2 个答案:

答案 0 :(得分:1)

选项一Constructor Injection

public class Foo {
  List<String> actionPerformed(ActionEvent e) {
    List<String> newanimalArr = new ArrayList<>();
    .....
    return newanimalArr
}
...
public class Pets {
  private final List<String> array;
  public Pets(final List<String> array) {
    this.array = array;
  }
  void bar() {
    System.out.println(this.array);
  }
}
....
public static void main(String[] args) {
  Foo foo = new Foo();
  Pets pets = new Pets(foo.actionPerformed( new ActionEvent() ) );
  pets.bar();
}

选项二Getter-Setter Injection

public class Foo {
   private final List<String> newanimalArr;
   public Foo() {
     this.newanimalArr = new ArrayList<>();
   }  
   public void actionPerformed(ActionEvent e) {
    .....
   }
   public List<String> getNewanimalArr() {
      return new ArrayList<String>(newanimalArr);
   }
}
...
public class Pets {
  private List<String> array;
  public Pets() {
    this.array = Collections.<String>emptyList();
  }
  public void setArray(final List<String> array) {
    this.array = array;  
  }
  public void bar() {
    System.out.println(this.array);
  }
}
....
public static void main(String[] args) {
  Foo foo = new Foo();
  foo.actionPerformed( new ActionEvent() );
  Pets pets = new Pets();
  bar.setArray( foo.getNewanimalArr() );
  pets.bar();
}

另见Dependency Injection Patterns

答案 1 :(得分:0)

使用字段的实例变量创建Pet的类定义。在Java中,可以自定义为每个setXyz字段创建getXyzxyz。您还可以创建一个构造函数,在该构造函数中传递所有值并将它们分配给字段,这样可以最大限度地降低字段未填充的风险。 您正在创建的初始ArrayList不会添加太多用途,直接创建Pet个实例会更容易:

List<Pet> newArrivals = new ArrayList<>();
// get data from view fields and if necessary transform them to other objects such as:
LocalDate arrivedOn = LocalDate.parse(txtArrivaldate.getText(), DateTimeFormatter.ofLocalizedDate(FormatStyle.FormatStyle);
// create and add a new Pet object to the list
newArrivals.add(new Pet(.....));

public class Pet {
public enum Gender {
    FEMALE, MALE
}

private String givenName;
private String commonName;
private double price;
private Gender gender;
private String color;
private LocalDate arrivedOn;
private LocalDate soldOn;

public Pet() {
}

public Pet(String givenName, String commonName, double price, Gender gender, String color, LocalDate arrivedOn,
        LocalDate soldOn) {
    super();
    this.givenName = givenName;
    this.commonName = commonName;
    this.price = price;
    this.gender = gender;
    this.color = color;
    this.arrivedOn = arrivedOn;
    this.soldOn = soldOn;
}

public String getGivenName() {
    return givenName;
}

public void setGivenName(String givenName) {
    this.givenName = givenName;
}

public String getCommonName() {
    return commonName;
}

public void setCommonName(String commonName) {
    this.commonName = commonName;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public Gender getGender() {
    return gender;
}

public void setGender(Gender gender) {
    this.gender = gender;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public LocalDate getArrivedOn() {
    return arrivedOn;
}

public void setArrivedOn(LocalDate arrivedOn) {
    this.arrivedOn = arrivedOn;
}

public LocalDate getSoldOn() {
    return soldOn;
}

public void setSoldOn(LocalDate soldOn) {
    this.soldOn = soldOn;
}

}