how do you declare a new int array?
like int[] array; ???
then how do you add to the array??
Printable View
how do you declare a new int array?
like int[] array; ???
then how do you add to the array??
You should learn the basis about Java, this is simple thing actually.
Code:public class IntArray {
public static void main(String[] args) {
int temp = 0; // Dummy value
int[] myArray; // Declaring an int type array
myArray = new int[10]; // Initialize the array and reserver the space
// for 10 elements.
// Adding elements to the array
for(int i = 0; i < myArray.length; i++) {
temp += 5;
myArray[i] = temp;
}
// Display elements
for(int j = 0; j < myArray.length; j++) {
System.out.println("Value of index " + j + " is " + myArray[j]);
}
}
}
thanks for replying!
what if i don't know the size of my array and i just want to be able to add an arbitrary amount to it.
instead of freeing up a fixed size space, how would i go about this?
You want to set the dynamic size. Arrays not allowed that, you have to follow another way. Read more about Java collections. There are lots of options.
Use an java.util.ArrayList. How? Find it out yourself, no spoon feeding. ;)
Actually you have lots of choices, ArrayList is just a one of them. As Supamagier says, finding yourself is your duty. We'll help you, just have atry.
else use list. it would be easier