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

将数组数据从一个类发送到另一个JAVA类,java,arrays,Java,Arrays,(我是一个初学者,所以这听起来很明显/缺乏信息。)我有一个不同宠物的属性列表,包括它们的名字、普通名字、动物的价格、性别、购买日期和出售日期等属性。该信息是从一个单独的类生成的,该类将信息数组添加到现有动物列表的数组中。本质上,我希望将数组发送到另一个类(称为Pets),以便将其添加到数组数组中。我理解这听起来可能令人困惑,但这是我唯一可以表达的方式,如果需要,我可以澄清任何事情。任何帮助都将是伟大的,因为我真的被卡住了,不知道如何发送它。这是在数组中生成my值的代码(使用文本框输入信息) pu

(我是一个初学者,所以这听起来很明显/缺乏信息。)我有一个不同宠物的属性列表,包括它们的名字、普通名字、动物的价格、性别、购买日期和出售日期等属性。该信息是从一个单独的类生成的,该类将信息数组添加到现有动物列表的数组中。本质上,我希望将数组发送到另一个类(称为Pets),以便将其添加到数组数组中。我理解这听起来可能令人困惑,但这是我唯一可以表达的方式,如果需要,我可以澄清任何事情。任何帮助都将是伟大的,因为我真的被卡住了,不知道如何发送它。这是在数组中生成my值的代码(使用文本框输入信息)

public void actionPerformed(ActionEvent e){
ArrayList NewanimalArr=新ArrayList();
字符串givenName=txtGivenname.getText();
字符串commonName=txtCommonName.getText();
字符串priceOf=txtPrice_1.getText();
字符串sexOf=txtSex.getText();
字符串coloroof=txtmaincolor.getText();
字符串dateOfA=txtArrivaldate.getText();
字符串datesell=txtSellingdate.getText();
NewanimalArr.add(givenName);
NewanimalArr.add(commonName);
NewanimalArr.add(priceOf);
NewanimalArr.add(sexOf);
NewanimalArr.add(颜色);
NewanimalArr.add(dateOfA);
NewanimalArr.add(datesell);
System.out.println(NewanimalArr);
}
});
然后将打印输入的生成信息,例如: [亚历克斯,狗,40.50,雄性,棕色,14/04/2015,14/12/2016]
然后如何将此数据发送到另一个类

使用字段的实例变量创建
Pet
的类定义。在Java中,可以自定义为每个
xyz
字段创建
setXyz
getXyz
。您还可以创建一个构造函数,在该构造函数中传递所有值并将它们分配给字段,这样可以最大限度地降低字段未填充的风险。 您正在创建的初始
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;
}
List newArrivals=new ArrayList();
//从视图字段获取数据,如有必要,将其转换为其他对象,例如:
LocalDate arrivedOn=LocalDate.parse(txtArrivaldate.getText(),DateTimeFormatter.ofcalizeddate(FormatStyle.FormatStyle);
//创建新宠物对象并将其添加到列表中
添加(新宠物(…);
公营宠物{
公共枚举性别{
女,男
}
私人字符串givenName;
私有字符串commonName;
私人双价;
私人性别;
私有字符串颜色;
私有LocalDate arrivedOn;
私有LocalDate-soldOn;
公共宠物(){
}
公共宠物(字符串givenName、字符串commonName、双倍价格、性别、字符串颜色、LocalDate arrivedOn、,
本地日期(soldOn){
超级();
this.givenName=givenName;
this.commonName=commonName;
这个价格=价格;
这个。性别=性别;
这个颜色=颜色;
this.arrivedOn=arrivedOn;
this.soldOn=soldOn;
}
公共字符串getGivenName(){
回报给我;
}
public void setGivenName(字符串givenName){
this.givenName=givenName;
}
公共字符串getCommonName(){
返回commonName;
}
public void setCommonName(字符串commonName){
this.commonName=commonName;
}
公开双价{
退货价格;
}
公共定价(双倍价格){
这个价格=价格;
}
公共性别{
返回性别;
}
公共性别(性别){
这个。性别=性别;
}
公共字符串getColor(){
返回颜色;
}
公共void setColor(字符串颜色){
这个颜色=颜色;
}
public LocalDate getArrivedOn(){
返回arrivedOn;
}
public void setArrivedOn(LocalDate arrivedOn){
this.arrivedOn=arrivedOn;
}
公共LocalDate getSoldOn(){
返回索尔顿;
}
public void setSoldOn(LocalDate soldOn){
this.soldOn=soldOn;
}

}

选项一
构造函数注入

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();
}
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();
}
另请参见

没有什么比“发送数据”更好的了。您可以制作
newAnimalArr
(旁注:请遵守命名约定)你的类的一个字段,可以让它在你的类中的任何地方都可以访问。但是我强烈建议你只看一看Java教程。没有理由因为超出你当前知识水平的事情而沮丧。阅读教程,一步一步地做。
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();
}