Printing array in another class

时间:2015-07-31 20:42:15

标签: java

I am fairly new at programming in Java. I just learned about arrays and did a couple of tests and ran into trouble.

    import java.util.Scanner;
class oranges {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        int user_input;
        whatever whateverObject = new whatever();
        System.out.println("Enter 1 to demonstrate Pythagorean Theorem. ");
        System.out.println("Enter 2 to find the maximum number.");
        System.out.println("Enter 3 to find the minimum number. ");
        System.out.println("Enter 4 to generate a random number.");
        System.out.println("Enter 5 to print array.");
        user_input = input.nextInt();
        switch (user_input) {
        case 1:
            whateverObject.pythagorean();
            break;
        case 2:
            whateverObject.max_num();
            break;
        case 4:
            whateverObject.ran_num();
            break;
        case 3:
            whateverObject.min_num();
            break;
        case 5:
            whateverObject.array_test();
            break;
        default:
            System.out.println("Sorry, try again.");
        }
        input.close();
    }
}

    import java.util.Scanner;
import java.util.Random;
public class whatever {
    public void pythagorean() {
        Scanner input = new Scanner(System.in);
        double first, second, last;
        String user_input;
        System.out.println("First edge: ");
        first = input.nextDouble();
        System.out.println("Second edge: ");
        second = input.nextDouble();
        System.out.println("Third edge: ");
        last = Math.pow(first, 2) + Math.pow(second, 2);
        System.out.println(Math.sqrt(last));
        System.out.println("Enter 'quit' to return.");
        user_input = input.nextLine();
        if (user_input.equals("quit")) {
            oranges.main(null);
        }
        input.close();
    }
    public void max_num() {
        Scanner input = new Scanner(System.in);
        int fnum, snum;
        String user_input;
        System.out.println("Enter the first number:");
        fnum = input.nextInt();
        System.out.println("Enter the second number: ");
        snum = input.nextInt();
        System.out.println(Math.max(fnum, snum));
        System.out.println("Enter 'quit' to return.");
        user_input = input.nextLine();
        if (user_input.equals("quit")) {
            oranges.main(null);
        }
    }
    public void ran_num() {
        Scanner input = new Scanner(System.in);
        Random dice = new Random();
        String user_input, user_input2;
        int ran_num;
        int counter = 69;
        while (counter < 300) {
        System.out.println("Enter to generate a random number!");
        user_input = input.nextLine();
        ran_num = 1+dice.nextInt(1000);
        System.out.println(ran_num);
        System.out.println("Enter 'quit' to return.");
        user_input2 = input.nextLine();
        if (user_input.equals("quit")) {
            oranges.main(null);
        }
        }

    }
    public void min_num() {
        Scanner input = new Scanner(System.in);
        int fnum, snum;
        String user_input;
        System.out.println("Enter the first number:");
        fnum = input.nextInt();
        System.out.println("Enter the second number: ");
        snum = input.nextInt();
        System.out.println(Math.min(fnum, snum));
        System.out.println("Enter 'quit' to return.");
        user_input = input.nextLine();
        if (user_input.equals("quit")) {
            oranges.main(null);
        }
    }
    public void array_test() {
        int test_array[] = new int[3];
        test_array[0]=56;
        test_array[1]=67;
        test_array[2]=138;
        test_array[3]=70;
        System.out.println(test_array[2]);
    }
}

I'm trying to calling a method that prints out the test_array array. But instead it says:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at whatever.array_test(whatever.java:76)
    at oranges.main(oranges.java:27)

What does this mean?

3 个答案:

答案 0 :(得分:3)

test_array is an int[3]. That means that it has 3 elements, indexed by 0, 1, and 2. If you say test_array[3] that is "out of bounds" because, well, you're beyond the boundary of the array.

Arrays are 0-based, which means that the first index is 0 (as you seem to realize already). The last element of an array is array.length - 1.

答案 1 :(得分:2)

int test_array[] = new int[3];

That means the array has got 3 indexes: 0, 1 and 2 You're trying to set a value for the 4th index at test_array[3]=70; - That's not possible

答案 2 :(得分:1)

int test_array[] = new int[3];

You are creating an array with 3 Integer values, which have the indexes of:

0    1    2

Calling test_array[3]=70; would be setting the fourth index (since 3 is the 4th value starting from 0):

new int [1]   [2]   [3]
===========================================
|INDEX|  0  |  1  |  2  |  Out of bounds  |
|VALUE|  56 |  67 | 138 |      70         |  -> ArrayIndexOutOfBoundsException
===========================================