Java dynamic array sizes?

前端 未结 18 2120
星月不相逢
星月不相逢 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:27

    In Java Array Sizes are always of Fixed Length But there is way in which you can Dynamically increase the Size of the Array at Runtime Itself

    This is the most "used" as well as preferred way to do it-

        int temp[]=new int[stck.length+1];
        for(int i=0;i

    In the above code we are initializing a new temp[] array, and further using a for loop to initialize the contents of the temp with the contents of the original array ie. stck[]. And then again copying it back to the original one, giving us a new array of new SIZE.

    No doubt it generates a CPU Overhead due to reinitializing an array using for loop repeatedly. But you can still use and implement it in your code. For the best practice use "Linked List" instead of Array, if you want the data to be stored dynamically in the memory, of variable length.

    Here's a Real-Time Example based on Dynamic Stacks to INCREASE ARRAY SIZE at Run-Time

    File-name: DStack.java

    public class DStack {
    private int stck[];
    int tos;
    
    void Init_Stck(int size) {
        stck=new int[size];
        tos=-1;
    }
    int Change_Stck(int size){
        return stck[size];
    }
    
    public void push(int item){
        if(tos==stck.length-1){
            int temp[]=new int[stck.length+1];
            for(int i=0;i

    File-name: Exec.java
    (with the main class)

    import java.util.*;
    public class Exec {
    
    private static Scanner in;
    
    public static void main(String[] args) {
        in = new Scanner(System.in);
        int option,item,i=1;
        DStack obj=new DStack();
        obj.Init_Stck(1);
        do{
            System.out.println();
            System.out.println("--MENU--");
            System.out.println("1. Push a Value in The Stack");
            System.out.println("2. Pop a Value from the Stack");
            System.out.println("3. Display Stack");
            System.out.println("4. Exit");
            option=in.nextInt();
            switch(option){
            case 1:
                System.out.println("Enter the Value to be Pushed");
                item=in.nextInt();
                obj.push(item);
                break;
            case 2:
                System.out.println("Popped Item: "+obj.pop());
                obj.Change_Stck(obj.tos);
                break;
            case 3:
                System.out.println("Displaying...");
                obj.display();
                break;
            case 4:
                System.out.println("Exiting...");
                i=0;
                break;
            default:
                System.out.println("Enter a Valid Value");
    
            }
        }while(i==1);
    
    }
    
    }
    

    Hope this solves your query.

提交回复
热议问题