1
class Main {
    public static void main(String[] args) {
        int x = 3;
        int [] a = new int [x];
        x+=5;
        System.out.println((x+=5));
        System.out.println(a.length);
    }
}

Why doesn't it effect the length of array "a" ? What am i doing wrong?

The thing is I was asked to add 5 to the length of an array using a loop. I can't even increase the length directly. It looks like a simple issue but I'm still in learning process. I did it yesterday but I don't remember how.

4
  • 1
    Your question is not clear, what do you mean by "add 5 to the length of an array"? Commented Oct 13, 2013 at 10:55
  • 2
    If you really want a "growable" array, you should be looking at the ArrayList class. Commented Oct 13, 2013 at 10:56
  • 1
    Please don't use side-effects unless they are necessary. Avoid doing things like System.out.println((x+=5));, which might cause you troubles in future. Let a "print" line simply print. Commented Oct 13, 2013 at 10:56
  • if you want the array length to be 8 at last, you should remove System.out.println((x+=5)); and add a = new int[x];. Commented Oct 13, 2013 at 10:57

5 Answers 5

6

You are just changing the value of x, you also have to reassign this value to change the array length.

        int x = 3;
        int[] a = new int[x];
        x += 5;
        a = new int[x];
        System.out.println(a.length);
Sign up to request clarification or add additional context in comments.

7 Comments

This is the right approach but it does not use a loop. I am puzzled about what the homework assignment expects because it does not really make sense.
While this may be able to create a new array with the specified size, it doesn't do anything with the elements that the old array might already have. Consider using an ArrayList instead. If this is just a homework, you should also be looking at System.arrayCopy.
@FlorianR.Klein , (x += 5) reassigns the variable x , and then uses the value stored at x for printing
@EricJablow , i was increasing the value in System.out so it increased value by 10, have edited the answer
I'll delete my comments then, though that will orphan @FlorianR.Klein's.
|
1

Merely changing value of x is not going to help,

You need top explicitly assign modified value back in array

Comments

1

You cannot increase the size of an array directly. You will learn about ArrayLists later. You can reassign an array variable to a new array; the length is not part of the array type. So, let's break this up into steps.

  1. Create an array of size 3.
  2. Report to the user the contents of the old array.
  3. Store the length in a variable.
  4. Add 5 to that variable.
  5. Create a new array with the new length.
  6. Copy the elements of the old array to the corresponding positions in the new array using a for loop, though System.arrayCopy is shorter.
  7. Assign the new array to the old array variable.
  8. Report to the user the contents of the changed old array.
  9. Look up the List and ArrayList documentation so you see how people do this in practice.

When you work on exercises like this, always break it down into individual steps first. When you program, break your methods into individual methods that do just one thing.

1 Comment

This is a good cooking recipe which can be used in conjunction with my answer.
1

Ok, I sum up to give you a more complete answer:

First, when you initialize an array, you assign a defined/finite chunk of memory to it. If you want to change the length of such an array in Java, you need to initialize a new one. There are no retrospective automatic reinitialization of arrays in Java. Other languages might do something something similar for you, but Java doesn't.

Second, you pass the x in your array-initialization by value which means, the functionalities, which create your array, receive the value ones but not the memory address. Methods in Java do it likewise: You pass values as parameters to them but they cannot see the variables directly as long as you don't explicitly reference to them in your local environment of each method. This is why no function, which accepts parameters, is directly influenced by changes of the variables from which values have been passed.

Third, if you try to change the size of an array often, you should consider using ArrayLists which include the functionality which you are looking for.

Forth, if you want to keep using arrays instead of something else and want to keep the array members/values, you still need to pass the old array content to a new initialized array. This can be achieved by looping through the old array.

Comments

0

First of all, what are you trying to achieve? The length of your will be equal to the x value during the array creation (second line at your code), if you want to use dynamic array you should consider to use java.util.ArrayList.

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.