Cj1m
Cj1m

Reputation: 815

Adding ints to an array in Java

I am trying to create a random number to add to an array (arr). I have looked around for answers but they all seem to not be working or be outdated. Here is what I have tried:

int[] arr = {};
int rand =  (int) Math.round(Math.random() * 100);
arr = append(arr, rand);

However, this doesn't seem to work as there is a red line under append saying "The method append(int[], int) is undefined for the type new ActionListener(){}". Help would be highly appreciated!

Upvotes: 1

Views: 72369

Answers (7)

Bonne Bogaert
Bonne Bogaert

Reputation: 17

I have created a function called generaterandomInt.

private float generateRandomInt(int min, int max) {
    return min + (max - min) * random.nextInt();
}

Now you just have to implement this into your loop

for (i=0; i<10; i++) {
  int rand = generateRandomInt(0, 100);
  arr[i] = rand;
}

Upvotes: 0

Seagrass
Seagrass

Reputation: 403

You would first declare your array like so:

int[] arr;

Do you know how many integers you will be storing?

arr = new int[10];

You would then use a for loop to go through each element in the array.

for (i=0; i<10; i++) {
  int rand = (int) Math.round(Math.random() * 100);
  arr[i] = rand;
}

From Arrays (Java Docs)

Hope this helps.

Upvotes: 8

mthmulders
mthmulders

Reputation: 9705

By default, the Java compiler looks for methods in the same class as where you're calling them from. In this case, it seems to be an anonymous ActionListener class, so that's where it looks for the append(int[], int) method, but it can't find it.

Back to the goal you want to achieve. An array of primitive types (as your int[] is) does not have any methods. So we need a little help.

First we can convert the array to a List<Integer>, by using Arrays.asList(T...). Next we can add new ints to that list by calling List.add(E). Finally we can convert that List back to an int[] by calling List.toArray(T[]). All in one, that would look like this:

int[] arr = {};
List<Integer> list = Arrays.asList(arr);
list.add((int) Math.round(Math.random() * 100));
arr = list.toArray(arr);

Upvotes: 0

ZillGate
ZillGate

Reputation: 1183

Why not using List?

List<Integer> arr = new ArrayList<Integer>();
int rand =  (int) Math.round(Math.random() * 100);
arr.add(rand);

Upvotes: 0

jh314
jh314

Reputation: 27792

In Java, arrays are fixed length, so you can't dynamically append to them.

If you want to append things to a list, you should use Arraylist.

List<Integer> arr = new ArrayList<Integer>();
int rand =  (int) Math.round(Math.random() * 100);
arr.add(rand);

Upvotes: 2

QI3it
QI3it

Reputation: 181

There is no way to 'append' elements to an array. You have to create a new array with the appropriate size, copy all the old elements and insert the new one(s). Another way would be to use an ArrayList.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

You cannot "append" to an array: although array elements in Java are mutable, the length of the array is set at creation time, and cannot change later on.

If you need a collection that can change in size, use ArrayList<Integer> to append as many elements as you need; after that, you can convert the list to an array.

Upvotes: 4

Related Questions