# How to add multiple values to array within class?

**URL:** https://discourse.processing.org/t/how-to-add-multiple-values-to-array-within-class/28294
**Category:** Beginners
**Created:** [March 6, 2021, 3:21am UTC](https://discourse.processing.org/t/how-to-add-multiple-values-to-array-within-class/28294 "2021-03-06T03:21:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)
#### Post date: [March 6, 2021, 3:21am UTC](https://discourse.processing.org/t/how-to-add-multiple-values-to-array-within-class/28294/1 "2021-03-06T03:21:55Z")

</div>

I have the following array:

```auto
class Foo {
  int[] myArray;
  int val1;
  int val2;
  int val3;
  int val4;

  Foo(int val1, int val2, int val3, int val4) {
    this.myArray = new int[4];
    myArray = val1, val2, val3, val4;
  }
}

```

But I get the error message “Syntax Error - Missing operator, semicolon, or ‘}’ near ‘,’?” How can I add values to my array? I read that the .append method is inefficient (and I’m using the array mostly for efficiency).

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 6, 2021, 3:36am UTC](https://discourse.processing.org/t/how-to-add-multiple-values-to-array-within-class/28294/2 "2021-03-06T03:36:26Z")

</div>

```auto
void setup() {
  println(new Foo(10, 20, 30, 40).myArray);
  exit();
}

class Foo {
  int[] myArray;

  Foo(int val1, int val2, int val3, int val4) {
    myArray = new int[] { 
      val1, val2, val3, val4
    };
  }
}

```

```auto
void setup() {
  println(new Foo(10, 20, 30, 40).myArray);
  exit();
}

class Foo {
  int[] myArray;

  Foo(int... vals) {
    myArray = vals;
  }
}

```

---

<div class="post-metadata">

### Author: ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)
#### Post date: [March 6, 2021, 5:42am UTC](https://discourse.processing.org/t/how-to-add-multiple-values-to-array-within-class/28294/3 "2021-03-06T05:42:20Z")

</div>

Are these two alterantive options @GoToLoop? Why can’t I specify the length of the array with `new int[4]`?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 6, 2021, 6:05am UTC](https://discourse.processing.org/t/how-to-add-multiple-values-to-array-within-class/28294/4 "2021-03-06T06:05:49Z")

</div>

> [@tea\_tree](#):
>
> Are these two alternative options?

Yes. 1st example is your fixed code. While 2nd 1 is an alternative using variable arguments.

> [@tea\_tree](#):
>
> Why can’t I specify the length of the array with `new int[4]` ?

The statement `this.myArray = new int[4];` is correct.

However the following `myArray = val1, val2, val3, val4;` isn’t valid Java syntax!

Once a Java array is created we’d have to initialize each index 1 by 1:

```auto
  Foo(int val1, int val2, int val3, int val4) {
    myArray = new int[4];

    myArray[0] = val1;
    myArray[1] = val2;
    myArray[2] = val3;
    myArray[3] = val4;
  }

```

Therefore my recommendation is to go w/ either of my 2 examples for a shorter code.

More about Java array creation & initialization:

> **[Arrays (The Java™ Tutorials \>        
            Learning the Java...](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)**
>
> This beginner Java tutorial describes fundamentals of programming in the Java programming language

---

<div class="post-metadata">

### Author: ![tea\_tree](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@tea\_tree](https://discourse.processing.org/u/tea_tree)
#### Post date: [March 8, 2021, 2:07pm UTC](https://discourse.processing.org/t/how-to-add-multiple-values-to-array-within-class/28294/5 "2021-03-08T14:07:44Z")

</div>

This works perfectly. Thanks 🙂
