+ 10
how do i create a array that allows me to add elements whenever i want to?
I get an error in my program. Because I need to initialize the number of elements I want in the array when I create the array. But the number of elements in the array will change every time in my program how do i create a array that allows me to add elements whenever i want to without initializing the number of elements when I create the array?
15 Réponses
+ 6
So you want to add an element to the front of the array?
Pretend this is in the main method:
int[] array = {2, 3, 4};
Now lets say I want to add 1 to the 0th index, without overriding 2.
I call the add method below:
array = add(array, 1)
Then I output my value to make sure it's working:
System.out.println(array[0]);
// now I create the method
static int[] add(int[] array, int value){
int[] temp = new int[array.length+1];
temp[0] = value;
// deep copy the rest
for(int i = 1; i < temp.length; i++)
temp[i] = array[i-1];
return temp;
}
Output: 1
The new array is:
{1, 2, 3, 4}
So, I just created a new array of larger size, putting the new value at the front of the new array.
If all you want to do is add or remove a bunch of elements, I would suggest linked list over arraylist or array. If you don't know either of those what I did with the array is great for practicing how to deal with these issues. Instead of avoiding the problem.
+ 5
I know there is vectors in c++. Is there vectors in java?
+ 5
Will this work?
ArrayList<int> list=new ArrayList<int>();
+ 5
No, arraylist needs a reference type in the diamond operator.
You can do this:
ArrayList<Integer> myList = new ArrayList<>();
Or
ArrayList<Integer> myList = new ArrayList<Integer>();
You can also put the initial size of the list in the parenthesis. The list will also grow by that size if it is exceeded.
Like this:
ArrayList<Integer> myList = new ArrayList<>(10);
import the arraylist class from java.util
Check what @Serena and @Merkrafter said.
+ 5
can I do this to access a element in the ArrayList?
ArrayList<int> list=new ArrayList<int>();
list[0] = 2;
+ 4
Take a look at the java.util.List interface. It has two implementations, java.util.ArrayList and java.util.LinkedList. They have your desired behavior.
+ 4
You can use the get() set() or add() methods.
get(int index)
Returns the element at the specified position in this list.
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
Extra useful methods:
remove(int index)
Removes the element at the specified position in this list.
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.
void removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
From:
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
+ 3
Yes.
https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
But i'd recommend arraylist instead of vector if you want that.
+ 3
😳
+ 2
@Serena
University/School (mostly)
😝
+ 1
@Serena Yvonne
Just check out the Collections framework. There's much more to see. ;))