1

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!

5
  • 1
    You should read a basic tutorial about Arrays in Java. Commented Jul 18, 2013 at 13:28
  • From the tutorial on arrays (docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html): An array is a container object that holds a fixed number of values of a single type. Read the tutorial. Commented Jul 18, 2013 at 13:28
  • Take a look to docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html to learn how arrays are used in Java Commented Jul 18, 2013 at 13:28
  • You should consider using an ArrayList instead. You can't change the size of the array once you have created it. Commented Jul 18, 2013 at 13:29
  • If you used an ArrayList and used the add() method then it would append to the end of the array automatically. Commented Jul 18, 2013 at 13:29

7 Answers 7

8

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.

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

1 Comment

No worries. This was my first contribution on stackoverflow and I'm happy to be of assistance. In the case of an array for which you are unsure of the length though, it may be wise to go with the ArrayList object instead, as other users have suggested.
4

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.

1 Comment

#append might be a method that creates a new array with sufficient size and appends whatever needs appended; the returned then is a reference to a new array
2

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);

Comments

0

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.

Comments

0

Why not using List?

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

Comments

0

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);

Comments

0

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;
}

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.