Java dynamic array sizes?

前端 未结 18 2475
星月不相逢
星月不相逢 2020-11-22 04:37

I have a class - xClass, that I want to load into an array of xClass so I the declaration:

xClass mysclass[] = new xClass[10];
myclass[0] = new xClass();
my         


        
18条回答
  •  执念已碎
    2020-11-22 05:25

    Yes, we can do this way.

    import java.util.Scanner;
    
    public class Collection_Basic {
    
        private static Scanner sc;
    
        public static void main(String[] args) {
    
            Object[] obj=new Object[4];
            sc = new Scanner(System.in);
    
    
            //Storing element
            System.out.println("enter your element");
            for(int i=0;i<4;i++){
                obj[i]=sc.nextInt();
            }
    
            /*
             * here, size reaches with its maximum capacity so u can not store more element,
             * 
             * for storing more element we have to create new array Object with required size
             */
    
            Object[] tempObj=new Object[10];
    
            //copying old array to new Array
    
            int oldArraySize=obj.length;
            int i=0;
            for(;i

提交回复
热议问题