[英]Object ArrayList within Object ArrayList extended by class java
I have created a class with a group of objects which contain strings and booleans named " Accessories " 我创建了一个包含一组对象的类,这些对象包含名为“ 附件 ”的字符串和布尔值
then created ArrayList class which is then added to the list named " AccessoriesList ", from there more data is inputted. 然后创建ArrayList类,然后将其添加到名为“ AccessoriesList ”的列表中,从那里输入更多数据。
I then created an Accessories object to receive to data from the ArrayList, using a for loop. 然后,我使用for循环创建了一个附件对象,以从ArrayList接收数据。 this still responses as null.
这仍然为null。
I have looked around and found that the most common problem is that the variables have not been initialized. 我环顾四周,发现最常见的问题是变量尚未初始化。 So i tried and still getting the same result
所以我尝试并且仍然得到相同的结果
so here is the accessories class 所以这是配件课
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;
}
Here is the AccessoriesList class 这是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;
}
Instead of receiving the information, I receive the hash-code. 我没有收到信息,而是收到哈希码。
I am trying to throw an Accessories Object(orginal) from an ArrayList, into another Accessories Object(new). 我试图将一个ArrayList中的附件对象(原始)放入另一个附件对象(新)中。 i am trying to pull the data from the Accessories Object(new)
我试图从附件对象(新)中提取数据
You have two problems: 您有两个问题:
First, your constructor never copies the properties you pass to it into the class: Accessories(String Accessoriesname, boolean cold, boolean warm, boolean hot, boolean rain, boolean snow, boolean ice, boolean formal, boolean informal) { } 首先,构造函数永远不要将传递给它的属性复制到该类中:附件(字符串附件名称,布尔冷,布尔温暖,布尔热,布尔雨,布尔雪,布尔冰,布尔正式,布尔非正式){}
Think of a constructor as a method call with variable parameters: in this case you do nothing between your curly braces {
and }
. 将构造函数视为带有可变参数的方法调用:在这种情况下,您在花括号
{
和}
之间不执行任何操作。 Java provides you with the this
keyword to refer to a property of the class instance. Java为您提供了
this
关键字来引用类实例的属性。 So you need to explicitly copy the parameters passed to your constructor to the properties of your class instance: 因此,您需要将传递给构造函数的参数显式复制到类实例的属性中:
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
}
Second, because this line of code is concatenating a String with an object, it will call the .toString() method on your Accessories object: 其次,由于此行代码将一个String与一个对象串联在一起,因此它将在您的Accessories对象上调用.toString()方法:
System.out.println("this is the Accessoriesist : " + Accessoriesist.get(i));
The default implementation of the .toString()
method is inherited from the Object superclass. .toString()
方法的默认实现是从Object超类继承的。 If you'd like to override it, just add a method to your class with the same method signature as Object.toString()
如果您想重写它,只需将一个方法添加到您的类中,该方法具有与
Object.toString()
相同的方法签名
public String toString() {
StringBuilder sb = new StringBuilder(this.name);
if (ice) {
sb.append(" (ice)")
}
// other properties
return sb.toString()
}
A few final notes: 最后几点注意事项:
ArrayList<Accessories> Accessoriesist
would be ArrayList<Accessories> accessoriesList
following this convention. ArrayList<Accessories> Accessoriesist
将是ArrayList<Accessories> accessoriesList
。 enum
called something like Properties
and to have your Accessories class contain a List. Properties
的enum
并让您的Accessories类包含一个List。 Welcome to Java! 欢迎使用Java!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.