David Vandenbroucke
David Vandenbroucke

Reputation: 114

String[] or ArrayList in Constructor?

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.

Upvotes: 1

Views: 4102

Answers (7)

Nikolay Antipov
Nikolay Antipov

Reputation: 920

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");

.

Upvotes: 2

Pablo
Pablo

Reputation: 3673

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");

Upvotes: 2

SJuan76
SJuan76

Reputation: 24895

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

Upvotes: 2

Algorithmist
Algorithmist

Reputation: 6695

You could this in your constructor

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

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

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

Upvotes: 2

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116306

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.

Upvotes: 2

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299218

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));
}

Upvotes: 2

Botz3000
Botz3000

Reputation: 39660

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.

Upvotes: 3

Related Questions