简体   繁体   中英

In Java, Why cannot I insert an integer more than once, cannot print out after inserting one integer and cannot empty array with imported libraries?

After my previous question on my switch statement, the switch statement works great, but it caused several problems I came across.

1.) When I attempted to insert integer(s) to my array, it works when I typed the first input of one integer, it seemed to work on Eclipse, but after typing the 2nd input or input with 2 different integers. The error is shown below after my explanations.

2.) When I attempted to print out my array after inserting the first integer to test if it works or not, it's no good neither. Maybe there is something wrong with my ListArray class? The error is shown below after my explanations and it is same as my first problem.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at <my personal infomation>.ListArray.add(ListArray.java:24)
at <my personal information>.main.main(main.java:32)

3.) I know that .clear() is a general function to clear an array, but it is caused as an error on Eclipse, even I imported 3 libraries:

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;

the main code:

public abstract class main 
{
    public static void main(String[] args) 
    {
        ListArray list = new ListArray();

    algorithms Alg;

    Scanner s = new Scanner(System.in);

    printCommands();

    while(s.hasNext())
    {
        String command = s.next();
        char ch = s.next().charAt(0);

        switch(command)
        {
            case "c":
                list.clear(); //I have found other resources that used clear() as a built in function, but Eclipse asks me to create a method?
            return;

            case "a":
                ch = s.next().charAt(0);
                list.add(ch);
            break;

            case "d":
                System.out.println(list.toString());
            break;

            case "q":
                s.close();
                break;

            default:
              System.out.println("Invalid Command. Use one of the given commands.");
              printCommands();
              break;
        }
    }
}

my ListArray class

import java.util.Arrays;

public class ListArray 
{    
    private static final int MAX_VALUE = 0;
    private Object[] myStore;
    private int actSize = 0;

    public ListArray()
    {
        myStore = new Object[MAX_VALUE];
    }

public void add(Object obj)
{
    int x = MAX_VALUE;

    if(myStore.length-actSize <= x)
    {
        increaseListSize();
    }
    myStore[actSize++] = obj;
}

public int size()
{
    return actSize;
}

private void increaseListSize()
{
    myStore = Arrays.copyOf(myStore, myStore.length*2);
    //System.out.println("\nNew length: "+myStore.length);
}
}

This line

ListArray list = new ListArray();

Says that you are declaring variable with name list, which type is ListArray and you create an instance by calling contructor with no parameter.

The thing is - the TYPE is ListArray. No more, no less. It is your class, hence it does have only method inherited from Object (like equals, toString) and then only methods YOU create.

If you do not create method clear, it does not have one.

If you want to use standard Java classes, you have to declare variable which type is one.

Like List<Character> x = new ArrayList<Character>();

You get an ArrayIndexOutOfBoundsException because the size of the array is zero.

Your code (in the add method in ListArray ) notices that the array is too small, so it calls increaseListSize to double the size... but double zero is still zero.

Then it tries to assign the first element in this zero-length array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM