简体   繁体   中英

Can I put multiple values in object?

I whant an object with multiple values and in one value a list of Strings. for example a cookbook: The recipes are the objects stored in an arraylist. Every object should have a name, a boolean (isVeggie) and a list of all the ingredients and the associated quantities.

I thought of other arraylist inside the object, but I can´t access the list on the object.

public String name;
public double cost;
public boolean isClassic;
public boolean isVeggie;
public boolean isVegan;
protected ArrayList<String> ingredients  = new ArrayList<String>();

    
public recipes (String name, double cost, boolean isClassic, boolean isVeggie, boolean isVegan,
        ArrayList<String> ingredients) {
    super();
    this.name = name;
    this.cost = cost;
    this.isClassic = isClassic;
    this.isVeggie = isVeggie;
    this.isVegan = isVegan;
    this.ingredients = ingredients;

Of course you can create arrays or lists as a property inside another object. Consider the following example:

public class User {
    private String name;
    private List<String> nicknames;

    public User(String name, List<String> nicknames) {
        this.name = name;
        this.nicknames = nicknames;
    }

    public String getName() {
        return name;
    }

    public List<String> getNicknames() {
        return nicknames;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNickNames(List<String> nicknames) {
        this.nicknames = nicknames;
    }
}

Class User contains a list of nicknames. You can create a User object and access it's properties like the following:

User user = new User("RealName", Arrays.asList("nickname1", "nickname2"));
List<String> userNicknames = userr.getNicknames();

tl;dr

Define a record . Specify List as one of the member fields.

public record Recipe ( String name, double cost, boolean isClassic, boolean isVeggie, boolean isVegan, List< String > ingredients ) {}

Populate with List.of .

new Recipe(                     // Defined as `public record Recipe`. 
    "Beans & Rice", 
    0.99d, 
    true , 
    true, 
    true, 
    List.of( "beans", "rice" )  // `List.of` produces an object of type `List` using an unspecified concrete class. 
) 

Access the list of ingredients.

myRecipe.ingredients() 

Details

The Answer by Sergey is correct, and should be accepted. I will add a few thoughts.

Naming conventions

Mind the naming conventions in Java, as they make your code easier to read. So your class name should start with an uppercase letter. And naming in the singular makes more sense in your case. So: Recipe .

BigDecimal

In real work, never use a floating-point type where accuracy matters. Floating-point technology trades away accuracy for speed of execution.

So for money and such, use BigDecimal class rather than double .

Polymorphism: Use more general interface/superclass

Define your ingredients list property as the more general interface List rather than committing to it always being the concrete class ArrayList .

Unmodifiable list

Populate that ingredients list with an unmodifiable list if you do not intend to allow the calling programmer to make alterations.

Record

If the primary purpose of your class is to transparently and immutably carry data, then define the class as a record . Merely declare your member fields. The compiler implicitly creates the constructor, getters, equals & hashCode , and toString .

Your entire class definition becomes this one line:

public record Recipe ( 
    String name, 
    BigDecimal cost, 
    boolean isClassic,  
    boolean isVeggie, 
    boolean isVegan, 
    List<String> ingredients 
) {}

Example

Example usage.

Recipe beansAndRice = new Recipe( "Beans & Rice", new BigDecimal( "0.99" ), true , true, true, List.of( "beans", "rice" ) ) ;

To access the list of ingredients, call the implicitly generated accessor method whose name is the name of the property (no get prefix).

List< String > ingredients = beansAndRice.ingredients() ;

To make a modifiable copy of the list, pass to constructor of ArrayList .

List< String > ingredientsToModify = new ArrayList<>( ingredients ) ;

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