Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing Arrays/Reverse Array

So I have a question regarding printing arrays. These methods receive data from an array created from a file. The output should be 10 integers long in every row for as many integers are contained in the file. Let's say that the file contains {0, 1, 2, 3, 4, 5}, the output should be:

0   1   2   3   4   5

The first method works completely fine. The second method returns an error which I'll include down below. Can anyone help me figure out what's wrong? I've tried googling it but still don't understand. Here's the code:

public static void printArray(int[] array){
            System.out.println("Printing array: ");
            for (int i = 1; i<array.length+1; i++){
                    System.out.printf("%7d", array[i-1]);
                    if (i%10==0){
                            System.out.println();
                    }

            }
            System.out.println();
}
public static void reverseArray(int[] array){
            System.out.println("Printing reversed array: ");
            int a=0;
            for (int i = array.length; i>-1; i--){
                    System.out.printf("%7d", array[i]);
                    a++;
                    if (a%10==0){
                            System.out.println();
                    }
            }
            System.out.println();
}

Here's the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at Paniagua_ArrayProcessing.reverseArray(Paniagua_ArrayProcessing.java:49)
    at Paniagua_ArrayProcessing.main(Paniagua_ArrayProcessing.java:8)

Thanks for any help! Hopefully it's only a simple problem.

Edit: This is in java btw.

like image 673
a12b23c34 Avatar asked Mar 16 '14 19:03

a12b23c34


2 Answers

An array goes from 0 to length-1. Change your code to:

for (int i = array.length-1; i>-1; i--){

And the code will work.

You can read more about this here: Java Language Basics: Arrays

like image 166
durron597 Avatar answered Sep 29 '22 18:09

durron597


An array starts at index 0 and ends with array.length - 1. Thats the reason why you get an ArrayIndexOutOfBoundsException during access of array.length' entry of the array (which is one behind the last entry.

public static void printArray(int[] array){
    System.out.println("Printing array: ");
    for (int i = 0; i < array.length; i++){
        System.out.printf("%7d", array[i]);
        if (i % 10 == 0){
            System.out.println();
        }
    }
    System.out.println();
}

Futhermore your check for the 10th entry in the reverse array print is not correct, a was reset every time.

public static void reverseArray(int[] array){
    System.out.println("Printing reversed array: ");
    for (int i = array.length - 1; i >= 0; i--){
        System.out.printf("%7d", array[i]);
        if ((array.length - i) % 10 == 0){
            System.out.println();
        }
    }
    System.out.println();
}
like image 25
foxylion Avatar answered Sep 29 '22 18:09

foxylion