user1293780
user1293780

Reputation: 63

Putting two arrays together

I am developing a game for android this game reads an int[] array and forms a map out of it

now I want to randomly add smaller arrays to this array but how can I do that I don't have any clue

I have searched here and I didn't find a good solution

Upvotes: 0

Views: 650

Answers (3)

MAC
MAC

Reputation: 15847

you can use

Vector<String> vec = new Vector<String>();
vec.add("a");
vec.add("b);

suppose this is your old vector(or array) with two data.

if you want to add more data or want to add another array then.

for(int i=0;i<arr.length;i++)
{
    vec.add(arr[i]);
}

you can also get length of vector using

    int i=vec.size();

Upvotes: 0

Alex
Alex

Reputation: 4473

In Java array does not change in size. You have to create new bigger/smaller one and copy data. This may help.

Upvotes: 0

Jack
Jack

Reputation: 133557

If you need to do it many times arrays are not a good choice, because they have fixed size and must be reallocated every time you increase their size. In addition you have to copy values from the old array to the new, bigger, one.

Take a look to ArrayList<Integer> instead.

If it's something you do just from time to time you should do something like the following

int[] oldArray = new int[Y];
int[] smallArray = new int[X];
int[] newArray = Arrays.copyOf(oldArray,X+Y);
for (int i = Y; i < X+Y; ++i)
  newArray[i] = smallArray[i-Y];

Upvotes: 1

Related Questions