Elec Hill
Elec Hill

Reputation: 19

Reversing Order of Strings in two dimensional array - Java

I'm trying to reverse the order of strings in an array. I get the error: Index 3 out of bounds for length 1. This works for int if I use numbers but not for words. Could you help me out?

public class Main {

    public static void main(String[] args) {
    // write your code here


        String [][] arr = {{"My"}, {"name" }, {"is"}, {"John"}};

        String arr1 = arr[0][3];
        String arr2 = arr[0][2];
        String arr3 = arr[0][1];
        String arr4 = arr[0][0];
        String arrAll = arr1+arr2+arr3+arr4;
        System.out.println(arrAll);
    }
}

Upvotes: 0

Views: 317

Answers (4)

Abra
Abra

Reputation: 20914

Think of a 2 dimensional array, in java, as a matrix that consists of rows and columns. In order to find out how many rows are in arr, use the following code.

arr.length

You will find that this will return 4.

To get the first row in arr, use the following code.

arr[0]

Similarly, to get the second row, use the following code.

arr[1]

Each row may have a different number of columns, although in the code in your question, each row has one column.

To get the number of columns in the first row, the code is

arr[0].length

To get a single element in a two dimensional array you need both the row index and the column index, in that order. Hence to get the element in the first column of the first row, use the following

arr[0][0]

Since there is only one column in the first row, the following will cause a ArrayIndexOutOfBoundsException to be thrown.

arr[0][1]

This explains why arr[0][3] is giving you an error. You are trying to access the element in the fourth column of the first row but the first row has only one column.

The following code will provide your desired result.

String[][] arr = {{"My"}, {"name" }, {"is"}, {"John"}};
int rows = arr.length;
System.out.println("rows = " + rows);
for (int i = 0; i < rows; i++) {
    System.out.println("row " + i + " columns = " + arr[i].length);
}

String arr1 = arr[3][0];
String arr2 = arr[2][0];
String arr3 = arr[1][0];
String arr4 = arr[0][0];
String arrAll = arr1+arr2+arr3+arr4;
System.out.println(arrAll);

Running the above code produces the following.

rows = 4
row 0 columns = 1
row 1 columns = 1
row 2 columns = 1
row 3 columns = 1
JohnisnameMy

Upvotes: 1

devReddit
devReddit

Reputation: 2947

You have an array of array of strings. So when you're accessing arr[0] you're accessing the first array of string {"My"}. So, the arr[0][3] tries to access the third element from the the first row of the array of array of string, which actually doesn't exists as the array of string has only one element length 1, hence you're getting the ArrayIndexOutOfBoundException.

If you're trying to reverse the sentence, you can work with only first index of the arraysarr[0],arr[1],arr[2] & arr[3].

Upvotes: 0

The_O
The_O

Reputation: 563

I believe you are trying to select the first element of each of the inner arrays.

So, first index you are giving must be the index of the inner array. Second one must be the index of the element in that inner array. In other words it is arr[innerArrayIndex][elementIndex] and since all of your inner arrays have only one element, second indexes must be zero.

    String arr1 = arr[3][0];
    String arr2 = arr[2][0];
    String arr3 = arr[1][0];
    String arr4 = arr[0][0];

Upvotes: 0

DEV
DEV

Reputation: 1726

if you are trying to print the words, you just have to :

public static void main(String[] args) {
// write your code here
String [][] arr = {{"My"}, {"name" }, {"is"}, {"John"}};

    String [] arr1 = arr[3];
    String [] arr2 = arr[2];
    String [] arr3 = arr[1];
    String [] arr4 = arr[0];
    String arrAll = arr1[0]+arr2[0]+arr3[0]+arr4[0];
    System.out.println(arrAll);
}

Upvotes: 0

Related Questions