• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please expain me about single and multi dimension array by a pictorial example?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic