0

So I know that Java treats arrays' sizes as immutable, but in other languages like PHP, I have been able to use [] to assign a value to the next index of an array:

PHP:

$arr = array();

//Arr looks like this
$arr => {}

$arr[] = "Value";

//Arr looks like this
$arr => {"Value"}

Is there a similar function in Java?

int[] arr = new int[0];
arr[] = 3;
arr[] = 4;

//arr => [3, 4];
 
4
  • 3
    Java arrays are not immutable, but they cannot be resized. You need ArrayList<T>. Commented Feb 3, 2014 at 23:54
  • I'm familiar with ArrayList, I just wanted to know if there is a dynamic workaround to creating a new array every time you want to change the size. Oh and I meant the size is immutable, not the whole object :P Commented Feb 3, 2014 at 23:55
  • @David The "dynamic workaround to creating a new array every time you want to change the size" is called ArrayList, in which you can preallocate however much space you want and let the library manage it for you. Commented Feb 3, 2014 at 23:57
  • @David: So you're saying, "I know you can't change the size, but is there any way for me to change the size?"? What? Commented Feb 3, 2014 at 23:59

5 Answers 5

6

In java you have to set the size of the Array when you create it

int[] arr = new int[2];

Then once the array is created, you can add values to a specific index like this:

arr[0] = 3;
arr[1] = 4;
//Now arr = {3, 4}

However, you cannot add a third value to the array arr because the size is fixed at 2. If you need to change the size of the array, an ArrayList would be better. You can just use the add() method and it will add values to the end of the array

ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(3);
arr.add(4);
arr.add(5);
//Now arr contains the values {3, 4, 5}
//You can continue you add values

EDIT: Another option is to use the Arrays.copyOf(int[] arr, int size)

int[] arr = {3, 4};
//arr contains the values {3, 4}
int[] arr2 = Arrays.copyOf(arr, 4);
//arr2 contains the values {3, 4, 0, 0}
Sign up to request clarification or add additional context in comments.

6 Comments

Ok so there is no way to change the size dynamically. I'd have to recreate the entire array each time I wanted to add a new index?
Yes. You can create a method that does that for you, but you would have to iterate through your entire array and add each value to your new, bigger array.
@David ArrayList does not recreate the entire array every time. It internally manages an array that is bigger than the logical capacity of the list, so that it has room to grow. It recreates the array only when needed.
@mdewitt Exactly what I wanted to know! Thanks so much! +1/accept
I think, you could also use Arrays.copyOf(...) to create a bigger array.
|
2

As has been said, Java arrays cannot be resized. However, if you used ArrayList<Integer>, you could use the add() function to get the desired result.

Comments

0

no. you have to use your own index or use an ArrayList and the add method

Comments

0

Almost. There is a shorthand syntax for array creation:

int[] arr = {3, 4};

Otherwise use a List which resizes automatically:

List<Integer> list = new ArrayList<>();
list.add(3);
list.add(4);
// etc

1 Comment

What's not useful about this answer?
0

In Java you have to set the number of elements in the array when you construct it. So you can't append more elements to the end of an array.

You can do the following to move to the next element in the array:

int[] arr = new int[2];
int = 0;
arr[i++] = 3;
arr[i++] = 4;

That works because i is incremented after each array element is assigned.

If you really want to append to the end of a list, you should use a Java collection class. Unfortunately you can't have a collection of primitive types (like int).

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.