•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Blog Entries

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Java:Tutorial - Arrays

array

8 replies to this topic

### #1 John

John

CC Mentor

• Moderator
• 4450 posts
• Location:New York, NY

Posted 09 December 2006 - 03:11 PM

In an earlier tutorial, we looked at how to store a single value to a variable, similarly an array is a named place to store a SET of values allowing you to group common types.

Sometimes, it becomes necessary to order variables in a list to spot trends:

Temperature on Sept 1 = 90
Temperature on Sept 2 = 87
Temperature on Sept 3 = 84

This is the ideal application for an array. All the variables and values are of the same type, integers, string, or what ever. Moreover there order and place in the list is important. Since an array allows you to store a “list” of values as a single “variable,” there must be a means of referencing each item. This reference is called an index. So therefore:

If
```Temp[1] = 90
Temp[2] = 87
Temp[3] = 84```

Then
```int x = 2;
Temp[ x ] = 87;```

At first, a concept that is difficult to grasp is that the index starts at 0. There for, an array with 5 elements contains the index values {0, 1, 2, 3, 4} but how do we make an array?

`int z[ ] = new int[5];`

A working example might look like this:
```int x;
int z[] = new int[10];
for (x = 0; x < 10; x++) {
z[x] = x;
}```

Therefore z[4] = 4! Arrays do not have to be numbers, for example:

```int x;
String myArray[ ] = new String[5];
myArray[0] = "hello";
myArray[1] = "this";
myArray[2] = "is";
myArray[3] = "an";
myArray[4] = "array";
for (x=0; x <= 4; x=x+1  )
{
System.out.println( myArray[x] );
}```

You can accomplish the same thing in less lines of code by using the following:
```int x;
String myArray[] = {"hello", "this", "is", "an", "array"};
for (x=0; x<=4; x++){
System.out.println( myArray[x] );
}```

If a single list of values isnt powerfull enough, we also have the ablilty to use miltidimensional arrays. This is sometimes refrered to as a matrix. Each index has its own set of indicies that are pared with it, for example:

```int matrix[][] = {{1,2,3,4}, {4,5,6,7}, {7,8,9,10}};
for (int i=0; i<=2; i++){
for (int j=0; j<=3; j++){
System.out.println( matrix[i][j] );
}
}```

• 3

### #2 Turk4n

Turk4n

???

• Expert Member
• 1919 posts
• Location:Sweden
• Programming Language:C, Java, PHP, Python, Bash
• Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 17 May 2008 - 07:56 AM

I mean Wow, normally people just make random explanations with no real sense; But you made one with quite awsome style, thank you for this great thread.
• 0

### #3 Maze

Maze

CC Regular

• Just Joined
• 32 posts

Posted 07 December 2008 - 03:00 AM

Thanks for nice share. It was really good.

I need some more examples about multi dimensional arrays. Could you explain and put some more examples about?

THanks
• 0

### #4 Guest_Jordan_*

Guest_Jordan_*
• Guest

Posted 07 December 2008 - 06:18 AM

I've never seen this tutorial or at least I cannot ever remember reading it. Very nice, thank you. +rep
• 0

### #5 miromtm

miromtm

CC Newcomer

• Just Joined
• 18 posts

Posted 26 December 2008 - 01:39 PM

WOW , man am very thankful ,
• 0

### #6 Turk4n

Turk4n

???

• Expert Member
• 1919 posts
• Location:Sweden
• Programming Language:C, Java, PHP, Python, Bash
• Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 26 December 2008 - 01:50 PM

WOW , man am very thankful ,

Good to see happy faces
• 0

### #7 whitey6993

whitey6993

• Just Joined
• 156 posts

Posted 28 December 2008 - 09:37 AM

Excellent tutorial for introducing arrays and multi-dimensional arrays.
• 0

### #8 Aroos

Aroos

CC Lurker

• Just Joined
• 1 posts

Posted 01 July 2009 - 11:12 PM

Thanks for nice share. It was really good.

I need some more examples about multi dimensional arrays java . Could you explain and put some more examples about?

THanks
• 0

### #9 Turk4n

Turk4n

???

• Expert Member
• 1919 posts
• Location:Sweden
• Programming Language:C, Java, PHP, Python, Bash
• Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 01 July 2009 - 11:28 PM

Thanks for nice share. It was really good.

I need some more examples about multi dimensional arrays java . Could you explain and put some more examples about?

THanks

Sure, just view this thread, it has great information about multi dimensional arrays !
http://forum.codecal...nal-arrays.html
Credit goes to Chili5
• 0

### Also tagged with one or more of these keywords: array

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download