[java] creating array without declaring the size - java

i've digging around about the same issue but i couldn't find the same as i had

i want to create an array without declaring the size because i don't know how it will be !

to clear the issue i'd like to give you the code that i'm looking up for

public class t
{
 private int x[];
 private int counter=0;
 public void add(int num)
 {
   this.x[this.counter] = num;
   this.counter++;
 }
}

as you see the user could use the add function to add element to the array 10000 times or only once so it's unknown size

This question is related to java arrays size declare

The answer is


Once the array size is fixed while running the program ,it's size can't be changed further. So better go for ArrayList while dealing with dynamic arrays.


You might be looking for a List? Either LinkedList or ArrayList are good classes to take a look at. You can then call toArray() to get the list as an array.


I think what you really want is an ArrayList or Vector. Arrays in Java are not like those in Javascript.


How about this

    private Object element[] = new Object[] {};

As others have said, use ArrayList. Here's how:

public class t
{
 private List<Integer> x = new ArrayList<Integer>();

 public void add(int num)
 {
   this.x.add(num);
 }
}

As you can see, your add method just calls the ArrayList's add method. This is only useful if your variable is private (which it is).


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to size

PySpark 2.0 The size or shape of a DataFrame How to set label size in Bootstrap How to create a DataFrame of random integers with Pandas? How to split large text file in windows? How can I get the size of an std::vector as an int? How to load specific image from assets with Swift How to find integer array size in java Fit website background image to screen size How to set text size in a button in html How to change font size in html?

Examples related to declare

How to declare an ArrayList with values? Declaring array of objects creating array without declaring the size - java How do I use variables in Oracle SQL Developer? SELECT INTO Variable in MySQL DECLARE causes syntax error?