Create an array without declaring size – Java
•
Java
I've been discussing the same problem, but I can't find the same one as me
I want to create an array without declaring the size, because I don't know how it will be!
To clear the code I want to give you, I'm looking for a problem
public class t { private int x[]; private int counter=0; public void add(int num) { this.x[this.counter] = num; this.counter++; } }
When you see that the user can use the add function to add an element to the array 10000 times or only once, so its size is unknown
Solution
Using Java util. ArrayList or LinkedList is the usual method It's impossible to have arrays because I know
Example:
List<Float> unindexedVectors = new ArrayList<Float>(); unindexedVectors.add(2.22f); unindexedVectors.get(2);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码