0

When i was implementing my codes in jtable in swing, i realized from their tutorial website. They use[][] as the code shown below. Ive tried searching throughout to find the use case but i am unable to find. The only link i found was https://www.geeksforgeeks.org/java-swing-jtable/

String arr_a[][] = {{"HelloWorld 1"},{"HelloWord2"}};

Currently, i am working on creating an object array to place it insde data..

ArrayList<String> Arr = new ArrayList<String>();
    Arr.add("Farid");
    Arr.add("Farid_2");

    Object[] DName = new MyData[Arr.size()];
        for(int i = 0; i < DName.length;i++){
            DName[i] = new MyData(Arr.get(i));
            System.out.println(DName[i].toString());
        }



 //-- How do we implement DName in data[][]

        String data[][];
        ....
        ....

For an example , it have to be in this form to display the values in jtable based on what is shown in the tutorial website

String data[][] = {{"test_1"}{"test_2")};

Thank you in advance

  • 3
    `String[][] name` & `String name[][]` are equivalent in Java (`String[] name[]` will also work). I think `String[][] name` is more readable. – Eran Jun 10 '19 at 09:08

2 Answers2

1

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Under section "Declaring a Variable to Refer to an Array"

In the oracle tutorials it tells us that both declarations are valid, but the String data[][] is discouraged. It doesn't give us a reason why it is discouraged but I presume it is simply to have a standard convention - code is much easier to read when we write it all the same after all

Similarly, you can declare arrays of other types:

byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;

You can also place the brackets after the array's name:

// this form is discouraged
float anArrayOfFloats[];
However, convention discourages this form; the brackets identify the array type and should appear with the type designation.
user27158
  • 297
  • 1
  • 5
  • 1
    I think it is discourage because it is "misleading" the array is an array of `String` not of data; therefore `String` should be marked as an array using `[]` not the variable name. – Gavin Jun 10 '19 at 10:06
  • @Gavin That's a fair point. I always assumed it was like having whitespace in the right place - it makes the code easier to understand at a glance, but I like your explanation more – user27158 Jun 10 '19 at 14:18
0

It's called Two dimensional array and there are no implementation Differences between String[][] name & String name[][]

Example to instantiate Multidimensional Array in Java

int[][] arr=new int[3][3];//3 row and 3 column

Example to initialize Multidimensional Array in Java

arr[0][0]=1;  
arr[0][1]=2;  
arr[0][2]=3;  
arr[1][0]=4;  
arr[1][1]=5;  
arr[1][2]=6;  
arr[2][0]=7;  
arr[2][1]=8;  
arr[2][2]=9; 

Example of Multidimensional Java Array Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional array.

//Java Program to illustrate the use of multidimensional array

class Testarray3{  
public static void main(String args[]){  
//declaring and initializing 2D array  
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};  
//printing 2D array  
for(int i=0;i<3;i++){  
 for(int j=0;j<3;j++){  
   System.out.print(arr[i][j]+" ");  
 }  
 System.out.println();  
}  
}}  
SandOfTime
  • 692
  • 6
  • 15