4

I'm having trouble reassigning values in an array.

    public static void main(String[] {

            int[] arrayOfIntegers = new int[4];
            arrayOfIntegers[0] = 11;
            arrayOfIntegers[1] = 12;
            arrayOfIntegers[2] = 13;
            arrayOfIntegers[3] = 14;

            arrayOfIntegers = {11,12,15,17};

    }

Why am I unable to reassign values in the manner that I've attempted? If I can't do it, why can't I do it?

1

3 Answers 3

5

Why am I unable to reassign values in the manner that I've attempted? If I can't do it, why can't I do it?

Because Java doesn't have destructuring assignment like some other languages do.

Your choices are:

  1. Assign a new array to the array variable as shown by Rohit and Kayaman, but that's not what you asked. You said you wanted to assign to the elements. If you assign a new array to ArrayOfIntegers, anything else that has a reference to the old array in a different variable or member will still refer to the old array.

  2. Use System.arraycopy, but it involves creating a temporary array:

     System.arraycopy(new int[]{11,12,15,17},
                      0,
                      ArrayOfIntegers,
                      0,
                      ArrayOfIntegers.length);
    

System.arraycopy will copy the elements into the existing array.

6
  • I had some quick questions and want to make sure I get this right. In Part 1 of your reply, when you say "anything else that has a reference to the old array", are you talking about subclasses that would reference ArrayOfIntegers? In regards to how System.arraycopy works, ArrayOfIntegers begins as [11,12,13,14]. Then a temporary array is created using System.arraycopy which replaces elements in the original 'ArrayOfIntegers'? How is this different from deconstructing assignment? Commented Aug 17, 2013 at 22:03
  • Also, what are "deconstructing assignments"? I thought assignments were simply changing the value of a variable and that the only "special" type of assignment as initialization since it's the first assignment. Commented Aug 17, 2013 at 22:10
  • @TomFang: A "deconstructing assignment" is where you have some kind of compound variable on the left-hand side and a compound value on the right-hand side, and each value on the right-hand side is assigned to the corresponding portion left-hand side variable. For instance, in languages that support them, you could literally have int a; int b; (a, b) = (1, 2); resulting in a being 1, and b being 2. The thing on the right is "deconstructed" into individual parts, and then assigned to the individual parts identified on the left. This is useful when the right-hand side is a return value. Commented Aug 18, 2013 at 0:37
  • @TomFang: "ArrayOfIntegers begins as [11,12,13,14]. Then a temporary array is created using System.arraycopy which replaces elements in the original 'ArrayOfIntegers'?" The temporary array isn't created by arraycopy, it's created by the array initializer we pass into arraycopy. "How is this different from deconstructing assignment?" It's very similar, which is why I pointed it out, because arraycopy takes each element of the source array and assigns it to the corresponding element of the destination array. Commented Aug 18, 2013 at 0:39
  • @TomFang: "...when you say "anything else that has a reference to the old array", are you talking about subclasses that would reference ArrayOfIntegers?" Not necessarily. It could be as simple as int[] a = ArrayOfIntegers; ArrayOfIntegers = new int[] { 11, 12, 15, 17}; Now, a refers to the old array, and ArrayOfIntegers refers to a new, completely separate, array. Whereas with arraycopy, they both continue to refer to the same array. Commented Aug 18, 2013 at 0:40
4

You need to provide the type of array. Use this:

arrayOfIntegers = new int[] {11,12,15,17};

From JLS Section 10.6:

An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.

If you are trying to re-assign array elements in some range, you can't do that with direct assignment. Either you need to assign values at indices individually, or use the way as given by @TJCrowder in his answer.

3

The correct syntax is:

arrayOfIntegers = new int[]{11,12,15,17};

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.