5

I'm currently on a self learning Java course and am completely stumped at one of my assignments, can anyone give me some pointers please? Bear in mind that I am completely new to Java so I need it to be as simple as.

The question: 3. Create a Java program called TwoDimArray and implement the following:

Create a two dimensional string array anArray[2][2].
Assign values to the 2d array containing any Country and associated colour. 

    Example:

    France Blue 

    Ireland Green 

Output the values of the 2d array using nested for loops.

The code I have come up with that is also not working:

public class TwoDimArray {
  public static void main(String[] args) {
    char Ireland = 0;
    char Green = 1;
    char England = 2;
    char White = 3;
    char firstArray[][] = {{Ireland},{Green}};
    char secondArray[][] = {{England},{White}};

    System.out.println();
    display(firstArray);
    System.out.println();
    display(secondArray);

  }

  public static void display(char a[][]) {
    for (char row = 0; row < a .length; row++) {
      for (char col = 0; col < a [row] .length; col++) {
        System.out.print(a[row][col] + " ");
      }

      System.out.println();
    }
  }
}

Can anyone give me some pointers? I can't seem to find anything that helps me on the web so thought I'd come here and ask. Thanks in advance.

4
  • 1
    "Create a two dimensional string array". Did you do that ? Commented Dec 3, 2013 at 18:49
  • you can make arrays of String, too. Commented Dec 3, 2013 at 18:51
  • I have no idea how I missed that at all, I'll get on that. Thanks guys. Commented Dec 3, 2013 at 18:53
  • I think this will help you [Two Dimensional String Array][1] [1]: stackoverflow.com/questions/19173396/… Commented Dec 3, 2013 at 18:58

1 Answer 1

8

Looks like this is what you want

    int columns = 2;
    int rows = 2;

    String[][] newArray = new String[columns][rows];
    newArray[0][0] = "France";
    newArray[0][1] = "Blue";

    newArray[1][0] = "Ireland";
    newArray[1][1] = "Green";

    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            System.out.println(newArray[i][j]);
        }
    }

Here I'll explain the code:

This declares the size of your new 2D array. In Java (and most programming languages), your first value starts at 0, so the size of this array is actually 2 rows by 2 columns

    int columns = 2;
    int rows = 2;

Here you are using the type String[][] to create a new 2D array with the size defined by [rows][columns].

    String[][] newArray = new String[columns][rows];

You assign the values by its placement within the array.

    newArray[0][0] = "France";
    newArray[0][1] = "Blue";

    newArray[1][0] = "Ireland";
    newArray[1][1] = "Green";

Looping through i would loop through the rows, and looping through j would loop through the columns. This code loops through all rows and columns and prints out the values in each index.

    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            System.out.println(newArray[i][j]);
        }
    }

Alternatively, assignment can be a one-liner:

    String[][] newArray = {{"France", "Blue"}, {"Ireland", "Green"}};

But I don't like this way, as when you start dealing with larger sets of data (like 10,000+ points of data with many columns), hardcoding it in like this can be rough.

Sign up to request clarification or add additional context in comments.

4 Comments

My homework for you is to not write it procedurally but write it objectively ;-). I think that's what the exercise would have wanted too.
Any reply? Feedback? Questions?
Np. Now do it objectively! I'll start you off with the instance variable as String[][] newArray.
Why is it String[][] newArray = new String[columns][rows]; and not String[][] newArray = new String[rows][columns];? I thought rows go before columns in initializing and accessing a 2d array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.