Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Jdk7 Arrays in java 7

Related Tutorials


 
 

Share on Google+Share on Google+

Arrays in java 7

Advertisement
This tutorial describes the concept of Arrays in java 7.

Arrays in java 7

This tutorial describes the concept of Arrays in java 7.

Arrays : Array is collection of similar type of data types where data are stored in continuous manner. You can say array is a container of fixed number of values of same type. At the time of array creation we define the length of array and the length is fixed after that we can't input element more than defined length of array. In Java 7, you can divide arrays in to two parts -

  • One- dimensional Array
  • Multi-Dimensional Array

One- dimensional Array :

In 1-D array there is one dimension to declare the data. In programming you can say it has only one index or one [].

An array is collection of similar type of data type. So whenever you need to store same type of data, use array of that type.
In array value are stored and indexed so you can access each individual value through its index value.
Suppose you want to access i indexed value of array A ,then just call A[i] which will print the desired value.

Example :

In this example we are creating int type array n and storing 5 int type elements. You can assign value to the array by user input or by defining in the braces separated by comma. By using length function you can calculate length of array. Next by using for loop we are printing value of array n.

Code-

public class ArrayExample {
public static void main(String[] args) {
int n[] = { 40, 34, 50, 60, 70 };
int l = n.length;
int i;
System.out.println("Given numbers in array : ");
for (i = 0; i < l; i++) {
System.out.print(n[i] + " ");
}
}
}

Output :

Given numbers in array : 
40 34 50 60 70 

Multi-Dimensional Array :

This kind of array having more than one dimension that is having different rows columns etc. Here we are discussing about 2-D arrays.

Java has build two dimensional array from one dimensional array therefore it is also called as arrays of arrays. It is usually visualized as a matrix, with rows and columns. It is having some interesting consequences:

  • It consists of rows and columns.
  • Rows and columns may be of different size.
  • Each row is an object that can be used independently.

Example :

In this example, we have defined two dimensional array with 11 rows and 11 columns to display the multiplication table from 1 to 10. To process a two dimensional array, we have used nested loops that means to run a loop in another loop. We have calculated the table by simply multiply the values of rows and columns .

class TwoDArrayExample {
public static void main(String[] args) {
int[][] array = new int[11][11];
int i, j, l;
l = array.length;
for (i = 1; i < l; i++) {
for (j = 1; j < l; j++) {
array[i][j] = i * j;
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
}
}

Output :

1	2	3	4	5	6	7	8	9	10	
2	4	6	8	10	12	14	16	18	20	
3	6	9	12	15	18	21	24	27	30	
4	8	12	16	20	24	28	32	36	40	
5	10	15	20	25	30	35	40	45	50	
6	12	18	24	30	36	42	48	54	60	
7	14	21	28	35	42	49	56	63	70	
8	16	24	32	40	48	56	64	72	80	
9	18	27	36	45	54	63	72	81	90	
10	20	30	40	50	60	70	80	90	100	
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2012

Related Tutorials

Discuss: Arrays in java 7  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com