1

I have to make a constructor for an ArrayList. So basically what I want is that this constructor makes an ArrayList.

    public Recipe(String[] modifications){

The problem is, it should give something like(example):

String[] modifications = [heatup, cooldown, mix, heatup]

Will the constructor work like this:

    Recipe(heatup, cooldown, mix, heatup)

or do I need to change it to somethign like:

    Recipe(modifications){
    this.myArrayList[1] = modifications[1]
    .
    .
    .
    }

thank you and sorry if there are some big mistakes in my code, still learning my Java code.

7 Answers 7

3

You can define it this way:

public Recipe(String... modifications)

This will give you a variable argument list, so your constructor will work like this: Recipe(heatup, cooldown, mix).

Inside of the constructor, you can use it just like an array.

Sign up to request clarification or add additional context in comments.

Comments

2

change the constructor to varargs:

public Recipe(String ... modifications){

and you can access modifications as String[] however, that's an array, not an ArrayList. If you want an ArrayList, do something like this:

private final List<String> list;
public Recipe(String ... modifications){
    this.list = new ArrayList<String>(Arrays.asList(modifications));
}

1 Comment

Oh man, I feel dumb :p I know the difference between Array and ArrayList, however, I so deep into it all right now, I keep messing things up in my mind :) I really thank everybody here :)!
2

You could make a constructor with varargs parameters:

public Recipe(String... modifications){...}

then you can call it like this:

Recipe(heatup, cooldown, mix, heatup);

Note though that you should be careful with varargs parameters if you (plan to) have other constructor(s) with potentially conflicting parameter list. However, if this is your only constructor, varargs should be fine.

Comments

2

You could this in your constructor

ArrayList<String> list=new ArrayList<String>();
        public Recipe(String[] modifications){

   // String a[]={"asas"};

            list.addAll(Arrays.asList(modifications));

Comments

2

The second one... of course you can use a for if you just want to copy the elements.

Also you can just copy the array passed by parameter into your class attribute / method variable, unless there is another motive not to do so.

Finally, remember that it would be something like Recipy(String[] modifications) {. You must define the type of the arguments.

And now really finally, myArrayList is a somewhat unfortunate name for an array (there is a class in the API called ArrayList).

Comments

2

What you want is to be able to pass an undetermined number of arguments:

public Recipe(String ... modifications) {

Inside the constructor, modifications is an array.

This will work with this:

new Recipe("heatup", "cooldown", "mix", "heatup");

and with this:

new Recipe();

and with this:

new Recipe("heatup", "mix");

Comments

2

You can use varargs:

public Recipe(String ... modifications) {
  // now you can use modifications as an array
  for (int f = 0; f < modifications.length; f++) {
     System.out.println("#" + f + ": " + modifications[f]);
  }
}

.

To use your class you just have to write:

  Recipe myRecipe = new Recipe("heatup", "mix");

.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.