2017-12-02 239 views
2

我已经创建了一类的基团,其包含字符串和布尔对象的命名扩展“配件对象的ArrayList通过级Java

然后创建ArrayList类,然后将其添加到名为“列表AccessoriesList“,从那里输入更多的数据。

然后我使用for循环创建了一个从ArrayList接收数据的附件对象。这仍然回应为空。

我环顾四周,发现最常见的问题是变量尚未初始化。所以,我想,仍然得到同样的结果

所以这里是配件

public static class Accessories { 

    Accessories(String Accessoriesname, boolean cold, boolean warm, boolean hot, boolean rain, boolean snow, boolean ice, boolean formal, boolean informal) { 
    } 
    String name =null ; boolean cold; boolean warm; boolean hot; boolean rain; boolean snow; boolean ice; boolean formal; boolean informal; 
} 

这里是AccessoriesList

public ArrayList createAccessories() { 
    ArrayList<Accessories> Accessoriesist = new ArrayList<Accessories>(); 
    Accessoriesist.add(new Accessories("Bag", true, true, true, false, false, false, true, true)); 
    Accessoriesist.add(new Accessories("Gloves", true, false, false, true, true, true, true, true)); 
    Accessoriesist.add(new Accessories("Hat", true, false, false, true, true, true, false, true)); 
    Accessoriesist.add(new Accessories("Poncho", false, true, true, false, false, false, false, true)); 
    Accessoriesist.add(new Accessories("Scarf", true, true, false, true, true, true, true, true)); 
    Accessoriesist.add(new Accessories("Sunglasses", false, true, true, false, false, false, true, true)); 
    Accessoriesist.add(new Accessories("Tie", true, true, true, true, true, true, true, true)); 

    Accessories getAccessories =null; 
    String getname = null; 
    for (int i = 0; i < Accessoriesist.size(); i++) { 
     getAccessories = Accessoriesist.get(i); 
     getname = getAccessories.name; 
     System.out.println("this is the name : " + getname); 
     System.out.println("this is the Accessoriesist : " + Accessoriesist.get(i)); 
    } 
    return Accessoriesist; 
} 

取而代之的接收信息,我收到散列码。

我想抛出一个配件对象(原始)从ArrayList,到另一个配件对象(新)。我试图拉从附件对象(新)

回答

0

的数据有两个问题:

首先,你的构造决不会复制你传递给它进级属性: 配件(字符串Accessoriesname,布尔布尔型布尔型布尔型布尔型布尔型布尔型布尔型非正式)

将构造函数想象成具有可变参数的方法调用:在这种情况下,您不需要做任何事情大括号{}。 Java为您提供了this关键字来引用类实例的属性。所以,你需要的参数传递给你的构造函数明确地复制到性能你的类的实例:

Accessories(String Accessories name, boolean cold, boolean warm, boolean hot, boolean rain, boolean snow, boolean ice, boolean formal, boolean informal) { 
    this.name = name 
    this.cold = cold 
    this.warm = warm 
    this.hot = hot 
    this.rain = rain 
    this.snow = snow 
    this.ice = ice 
    this.formal = formal 
    this.informal = informal 
} 

其次,因为该行的代码连接字符串与对象,它会调用的ToString()方法,在你的附件对象:

System.out.println("this is the Accessoriesist : " + Accessoriesist.get(i)); 

.toString()方法的默认实现从对象超类继承。如果您想覆盖它,只是用同样的方法添加签名的方法,以类为Object.toString()

public String toString() { 
    StringBuilder sb = new StringBuilder(this.name); 
    if (ice) { 
     sb.append(" (ice)") 
    } 
    // other properties 
    return sb.toString() 
} 

最后几点注意事项:

  • 这是在Java中传统使用的驼峰。对于课程,我们对首字母(MyClass)使用大写字母 ,对于第一个 字母(myClass)使用小写字母的类成员 变量和参数。所以你的ArrayList<Accessories> AccessoriesistArrayList<Accessories> accessoriesList遵循这个 约定。
  • 这可能是一个好主意,使你的所有不同 性能(冷,暖,冰,雪等)的enum叫什么 像Properties,并让您的配件类包含 列表。

欢迎来到Java!