creating array without declaring the size - java

后端 未结 6 1142
面向向阳花
面向向阳花 2020-12-13 16:29

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 w

6条回答
  •  遥遥无期
    2020-12-13 17:03

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

    public class t
    {
     private List x = new ArrayList();
    
     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).

提交回复
热议问题