Home > OS >  Unlimited Array
Unlimited Array

Time:11-20

I want to create an unlimited array, but there is some problem in my code that causes

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at JTB.main(JTB.java:13)

Here's my code with comments.

import java.util.Scanner;

public class JTB {
    public static void main(String args[]) {
        //variables and object(s)
        Scanner scanner = new Scanner(System.in);
        int jtb[] = {};
        int input = 0;
        int counter = 0;
        
        //Asks array input from user, 5 should be entered to cancel the loop.
        while(input != 5) {
            input = scanner.nextInt(); //line 13
            jtb[counter] = input;
              counter;
        }
        
        //prints out the array no. and the integer inside it.
        for(int counter1 = 0; counter1 < jtb.length;   counter1) {
            System.out.println("Array "   counter1   ": "   jtb[counter1]);
        }
    }
}

How do I make it work?

CodePudding user response:

Java does not support dynamic arrays. You can use ArrayList instead.

CodePudding user response:

You've defined jtb as an empty array. Any attempt to add to it will cause an ArrayIndexOutOfBoundsException.

You could give it a reasonable size instead of creating an empty array, but it will ultimately be limited. You could use a List instead.

List<Integer> jtb = new LinkedList<>();
int input = 0;

//Asks array input from user, 5 should be entered to cancel the loop.
while(input != 5) {
    input = scanner.nextInt(); //line 13
    jtb.add(input);
}

CodePudding user response:

If you want to get unlimited space use ArrayList, LinkedList etc check into java collection

Here down is ArrayList example

public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> data = new ArrayList<>();
        Integer counter = 0;
        Integer input = 0;
        while(input != 5) {
            input = scanner.nextInt();
            data.add(input);
            counter  ;
        }
        for(Integer extractData: data) {
            System.out.print("Print List:"   extractData   "\n");
        }
    }

Here down is LinkedList example

import java.util.*;
class Node {
    Node next;
    int data;

    Node(int data) {
        this.data = data;
    }
}

public class Main
{
    Node head;

    public void appendList(int data) {
        if (head == null) {
            head = new Node(data);
            return;
        }

        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new Node(data);
    }
    
    public void printList() {
        Node current = head;
        System.out.print("{ ");
        while (current != null) {
            System.out.print(current.data   ", ");
            current = current.next;
        }
        System.out.print("\b\b }");
    }
    public static void main(String args[]) {
        Main linkedList = new Main();
        linkedList.appendList(1);
        linkedList.appendList(2);
        linkedList.appendList(3);
        linkedList.appendList(4);
        linkedList.appendList(5);
        linkedList.printList();
    }
}

CodePudding user response:

In Java there is no dynamically growing nature of array. If you want a dynamically growing type of array then you have to go with the collection api. Use ArrayList to achieve goal.

      public static void main1(String args[]) {
            //variables and object(s)
            Scanner scanner = new Scanner(System.in);
            List<Integer> jtb = new ArrayList<>();
            int input = 0;
            
            //Asks array input from user, 5 should be entered to cancel the loop.
            while(input != 5) {
                input = scanner.nextInt(); //line 13
                jtb.add(input);
            }
            
            //prints out the array no. and the integer inside it.
            for(int index = 0; index < jtb.size();   index) {
                System.out.println("Array "   index   ": "   jtb.get(index));
            }
        }

CodePudding user response:

If you can't use collection api, you could implement a simpler one, but it's not unbounded, and the maximum array length should be Integer.max_value:

        //variables and object(s)
        Scanner scanner = new Scanner(System.in);
        int currentCapacity = 10;
        int jtb[] = new int[currentCapacity];
        int size = 0;
        int input = 0;
        int counter = 0;
        //Asks array input from user, 5 should be entered to cancel the loop.
        while(input != 5) {
            input = scanner.nextInt(); //line 13
            if (currentCapacity == size) {
                // Increases the capacity, for example, double the capacity
                int newCapacity = currentCapacity * 2;
                // Todo: consider overflow
                jtb = Arrays.copyOf(jtb, newCapacity);
                currentCapacity = newCapacity;
            }
            size  ;
            jtb[counter] = input;
              counter;
        }
  • Related