• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array

 
balajee annamalai
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please expain me about single and multi dimension array by a pictorial example?
 
Ahmed Kishk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1-Arrays are objects in Java that store multiple variables of the same type

2-Arrays can hold either primitives or object references

3- To Constructing One-Dimensional Arrays
int [] myArray;
myArray = new int[4];
you can assign four int values in this array "myArray"

4- To Constructing Two-Dimensional Arrays
int [][] myDimArray;
myDimArray= new int[4][4];

you can assign sixteen int values in this array and refer to them
with two indexs
this is a practical sample that generats random number and store it in the array and then print them again
//------------------------------------------
Random generator = new Random();
int [][] array = new int[4][4];
for(int i = 0 ; i < 4; i++)
for(int j = 0; j <4;j++)
array[i][j] = generator.nextInt(16);

for(int i = 0 ; i < 4; i++)
for(int j = 0; j <4;j++)
System.out.println(array[i][j]);
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, a "multi-dimensional" array is really just a single-dimension array whose elements are other single-dimension arrays.

The shorthand syntax provides a nice diagram...

Here you can see that i references an array containing 2 elements. The first element (i[0]) is an array containing 3 ints, and the second element (i[1]) is an array containing 4 ints.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic