简体   繁体   中英

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.

I then created an Accessories object to receive to data from the ArrayList, using a for loop. this still responses as 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

 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). 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. 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:

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

The default implementation of the .toString() method is inherited from the Object superclass. If you'd like to override it, just add a method to your class with the same method signature as 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:

  • It is conventional in Java to use CamelCase. For classes, we use a capital letter for the first letter (MyClass) and for class member variables and parameters we use a lowercase letter for the first letter (myClass). So your ArrayList<Accessories> Accessoriesist would be ArrayList<Accessories> accessoriesList following this convention.
  • It might be a good idea to make all of your different properties (cold, warm, ice, snow, etc) an enum called something like Properties and to have your Accessories class contain a List.

Welcome to Java!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM