Two dimensional Array - How to represent two-dimensional (2D) data ( table / grid ) in Java?

This article takes you through the most common and desirable ways to represent two-dimensional data (grid, table etc) in Java. In particular, this article introduces you to the basic data-structures available in Java to do this along with some illustrative source-code examples.

1. Introduction

Every element in the data set has two parameters / properties. Examples of day to day two-dimensional data strutures include a table, a grid, a martix etc.

1.1. Example

For ease of understanding lets consider a simple two-dimensional data-set - a table

 
Col 1
Col 2
Col 3
Row 1
1
2
3
Row 2
3
1
2
Row 3
2
3
1

Here the two-dimensions of every element in the data-set are the row and column.

2. Using two-dimensional arrays.

2.1. Introduction

"An array is a container object that holds a fixed number of values of a single type. Each item in an array is called an element, and each element is accessed by its numerical index." A two-dimensional array is one where each element is accessed by its two numerical indices.

The first step in using arrays is the initialisation bit. An important point to note about arrays is that you should specify the size of the array (only the row size is mandatory for 2-d arrays) during initialisation and that this size cannot be changed after array creation.

2.2. Initialisation

int[][] tableData = new int[3][3];

The above code initialises a 2-dimensional int array with 3 rows and 3 columns. An alternate way to initialise a 2-d array if you know its connect before hand is by...

        int[][] tableData  = new int[][]{
                        {1, 2, 3}, {3, 1, 2}, {2, 3, 1}
        };

 

2.3. Populating with data

Now lets populate it with the values from the above table.

        int[][] tableData  = new int[3][3];
       
        // Row 0 Col 0
        tableData [0][0] = 1;
        // Row 0 Col 1
        tableData [0][1] = 2;
        // Row 0 Col 2
        tableData [0][2] = 3;
       
        // Row 1 Col 0
        tableData [0][0] = 3;
        // Row 1 Col 1
        tableData [0][1] = 1;
        // Row 1 Col 2
        tableData [0][2] = 2;

        // Row 2 Col 0
        tableData [0][0] = 2;
        // Row 2 Col 1
        tableData [0][1] = 3;
        // Row 2 Col 2
        tableData [0][2] = 1;

2.3. Retrieving data

If you feel it was easy so far, then its get even more easier when it comes to extracting values out of the populated array. To retrieve a value at a given row and column you can just do

int tableValue = twoDArray[1][1];

To retrive all the values from a two-dimensional array your can use a for-loop.

        for (int row = 0; row < twoDArray.length; row++)
        {
                int[] tableRow = twoDArray[row];
                for (int col = 0; col < tableRow.length; col++)
                {
                        int tableValue = tableRow[col];
                        System.out.println("The table value at row - '" + row + "' col - " + col
                                        + " is " + tableValue);
                }
        }

 

 

Comments

i don't know how to ?

Write a Java program which does the following:

• Reads 9 integers into a 3 by 3 2-D array called R.
• Prints the sum of each of the diagonals of R.
• Swaps row 1 elements of R with row 2 elements of R.
• Prints the sum of each row
• Prints the sum of each column.

declaration

i don't know how to declare a two dimensional array....
how is it??

bnatey


توبيكات



توبيكات توبيكات



توبيكات اسلاميه



توبيكات اسلاميه



توبيكات حزينه


توبيكات حب


توبيكات رومانسيه



توبيكات اسماء



توبيكات اغاني



توبيكات منوعه



توبيكات مضحكه




توبيكات عتاب

توبيكات عتاب
توبيكات ساخره
توبيكات شعريه

توبيكات اجنبيه

توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه

توبيكات كريسماس

توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك

توبيكات

توبيكات

توبيكات اسلاميه

توبيكات حزينه

توبيكات حب

توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني

توبيكات منوعه

توبيكات مضحكه
توبيكات عتاب

توبيكات ساخره

توبيكات شعريه
توبيكات اجنبيه

توبيكات نكت

توبيكات جوال

توبيكات سعوديه

توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات

توبيكات


توبيكات فلاش


توبيكات روعه


توبيكات رائعه


منتديات

دردشة


العاب


العاب بنات

شات



توبيكات مصريه



توبيكات قطريه



دليل مواقع



تحميل
صور



مركز تحميل



برامج

كأس العالم
كأس العالم 2010

العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Two dimensional Array - How to represent two-dimensional (2D) data ( table / grid ) in Java? | Our website now yours! - Currenlty Java focussed.

Two dimensional Array - How to represent two-dimensional (2D) data ( table / grid ) in Java?

This article takes you through the most common and desirable ways to represent two-dimensional data (grid, table etc) in Java. In particular, this article introduces you to the basic data-structures available in Java to do this along with some illustrative source-code examples.

1. Introduction

Every element in the data set has two parameters / properties. Examples of day to day two-dimensional data strutures include a table, a grid, a martix etc.

1.1. Example

For ease of understanding lets consider a simple two-dimensional data-set - a table

 
Col 1
Col 2
Col 3
Row 1
1
2
3
Row 2
3
1
2
Row 3
2
3
1

Here the two-dimensions of every element in the data-set are the row and column.

2. Using two-dimensional arrays.

2.1. Introduction

"An array is a container object that holds a fixed number of values of a single type. Each item in an array is called an element, and each element is accessed by its numerical index." A two-dimensional array is one where each element is accessed by its two numerical indices.

The first step in using arrays is the initialisation bit. An important point to note about arrays is that you should specify the size of the array (only the row size is mandatory for 2-d arrays) during initialisation and that this size cannot be changed after array creation.

2.2. Initialisation

int[][] tableData = new int[3][3];

The above code initialises a 2-dimensional int array with 3 rows and 3 columns. An alternate way to initialise a 2-d array if you know its connect before hand is by...

        int[][] tableData  = new int[][]{
                        {1, 2, 3}, {3, 1, 2}, {2, 3, 1}
        };

 

2.3. Populating with data

Now lets populate it with the values from the above table.

        int[][] tableData  = new int[3][3];
       
        // Row 0 Col 0
        tableData [0][0] = 1;
        // Row 0 Col 1
        tableData [0][1] = 2;
        // Row 0 Col 2
        tableData [0][2] = 3;
       
        // Row 1 Col 0
        tableData [0][0] = 3;
        // Row 1 Col 1
        tableData [0][1] = 1;
        // Row 1 Col 2
        tableData [0][2] = 2;

        // Row 2 Col 0
        tableData [0][0] = 2;
        // Row 2 Col 1
        tableData [0][1] = 3;
        // Row 2 Col 2
        tableData [0][2] = 1;

2.3. Retrieving data

If you feel it was easy so far, then its get even more easier when it comes to extracting values out of the populated array. To retrieve a value at a given row and column you can just do

int tableValue = twoDArray[1][1];

To retrive all the values from a two-dimensional array your can use a for-loop.

        for (int row = 0; row < twoDArray.length; row++)
        {
                int[] tableRow = twoDArray[row];
                for (int col = 0; col < tableRow.length; col++)
                {
                        int tableValue = tableRow[col];
                        System.out.println("The table value at row - '" + row + "' col - " + col
                                        + " is " + tableValue);
                }
        }

 

 

Comments

i don't know how to ?

Write a Java program which does the following:

• Reads 9 integers into a 3 by 3 2-D array called R.
• Prints the sum of each of the diagonals of R.
• Swaps row 1 elements of R with row 2 elements of R.
• Prints the sum of each row
• Prints the sum of each column.

declaration

i don't know how to declare a two dimensional array....
how is it??

bnatey


توبيكات



توبيكات توبيكات



توبيكات اسلاميه



توبيكات اسلاميه



توبيكات حزينه


توبيكات حب


توبيكات رومانسيه



توبيكات اسماء



توبيكات اغاني



توبيكات منوعه



توبيكات مضحكه




توبيكات عتاب

توبيكات عتاب
توبيكات ساخره
توبيكات شعريه

توبيكات اجنبيه

توبيكات جديده
توبيكات بنات
توبيكات 2010
توبيكات اعياد
توبيكات عيد الاضحى
توبيكات مناسبات
توبيكات اعياد ميلاد
توبيكات رأس السنه

توبيكات كريسماس

توبيكات توبيكات
توبيكات كرة قدم
توبيكات كأس العالم
توبيكات مصر
توبيكات إلا رسول الله
توبيكات الاهلي
توبيكات الزمالك

توبيكات

توبيكات

توبيكات اسلاميه

توبيكات حزينه

توبيكات حب

توبيكات رومانسيه
توبيكات اسماء
توبيكات اغاني

توبيكات منوعه

توبيكات مضحكه
توبيكات عتاب

توبيكات ساخره

توبيكات شعريه
توبيكات اجنبيه

توبيكات نكت

توبيكات جوال

توبيكات سعوديه

توبيكات شخصيات فنيه
توبيكات ليلة العمر
توبيكات

توبيكات


توبيكات فلاش


توبيكات روعه


توبيكات رائعه


منتديات

دردشة


العاب


العاب بنات

شات



توبيكات مصريه



توبيكات قطريه



دليل مواقع



تحميل
صور



مركز تحميل



برامج

كأس العالم
كأس العالم 2010

العاب
العاب بنات
منتدي
فيديو بنات
برامج
مركز رفع صور
شات
دليل المواقع العربيه
العاب
العاب
دليل مواقع
توبيكات
كتب
مقالات
صور
منتدى أجنبي
منتديات

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options