0

I'm developing for the Android platform and, to simplify the question, I'm using pseudo-names for the entities.

I have an object array stuff[] of the class StuffClass[].

StuffClass stuff[]={
new StuffClass(Argument, argument, argument),
new StuffClass(argument, argument, argument)
};

I have an activity returning a result of three arguments that I want to then use to add a new object to stuff[]. I've done so as follows:

stuff[stuff.length]=new StuffClass(argument, argument, argument);

and I get ArrayOutOfBounds (Figured that would happen).

So how might I go about creating a new object in the stuff[] array?

2

5 Answers 5

1

Arrays are static you can't change size without creating a new one before. Instead of that you can use a dynamic data structure such as an ArrayList

Example:

List<MyType> objects = new ArrayList<>();
objects.add(new MyType());

Here you forget about array size.

Sign up to request clarification or add additional context in comments.

2 Comments

Are the method calls to ArrayLists the same as to Arrays? Like if I change the Array stuff[] to an Array List stuff[] will all my other calls to things like stuff.length, stuff[i].stuffMethod, etc be the same?
@NicolasMustaro An array has no methods. As you say to access to an object you do by position like stuff[i] in List you can do it the same, list.get(i). This tutorial may help you.
1

Array in Java is little bit special, it's length is fixed when it's initialized, you can not extend it later on.

What you can do is to create a new array, and use System.arraycopy to generate a new array, here's the sample code:

  String[] arr1 = new String[]{"a", "b"};
  String[] arr2 = new String[3];

  System.arraycopy(arr1, 0, arr2, 0, 2);
  arr2[2] = "c";

Comments

0

You cannot increase the size of an existing array. Once it's created, the size of the array is fixed.

You will need to create another bigger array and copy the items from the old array to the new array.

A better alternative is to use an ArrayList. When you add items to an ArrayList, the capacity will grow behind the scenes if needed; you don't have to worry about increasing the size.

Comments

0

you can use the ArrayList to do this

arraylist.add(object);

Comments

0

in java arrays are fixed length. you need to initialise them with the desired length.

Consider using a Collection such as ArrayList which will handle everything for you.

List<StuffClass> myList = new ArrayList<>();
myList.add(...);

Lists support similar behaviour to arrays ie:

myList.set(i, elem);
myArray[i] = elem;

elem = myList.get(i);
elem = myArray[i];

len = myList.size();
len = myArray.length;

You can then convert the list to an array.

StuffClass[] myArray = myList.toArray(new StuffClass[myList.size()]);

If you don't want to use lists consider using System.arrayCopy to create a new array with more elements.

read here for a good description.

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.