Arrays
Arrays
Arrays are a way to store a list of items that have the same primitive data type, the same
class.Each item on the list goes into its own numbered slot so that you can easily
access the information.
Once Array is created of certain type then it can only used to store and retrieve the
value of specified type.
Java implements arrays differently than some other languages do—as objects treated like
other objects.
To create an array in Java, you must do the following:
1. Declare a variable to hold the array.
2. Create a new array object and assign it to the array variable.
3. Store information in that array.
The following statements are examples of array variable declarations:
String[] requests;
Point[] targets;
float[] donations;
String targets[];
Point targets[];
Creating Array Objects
After you declare the array variable, the next step is to create an array object and assign
it to that variable. To do this:
1.use the new operator.
2.Initialize the contents of the array directly.
Because arrays are objects in Java you can use new operator to create an instance like:
String[] ename=new String[10];
This statement creates array with 10 elements Starting with ename[0].
You can also create array of primitive data types as of int,Boolean,char etc.
At the beginning when no value is allocated to array elements by default they
contain null values.
You can directly create array as:
String[] name={"roma","milano","cristy"};
That is dirctly specify values within curly braces but remember values must match
the specified data type.
Retrieving values from Arrays
The values stored in arrays can be retrieved directly by specifying index number of element
such as:
//System.out.println(ename[0]);
or we can retrieve them using a loop such as
/* for(int i=o;i
A multidimensional array has more than one dimension unlike simple arrays .
A Multidimensional array can be stated as array of arrays
Basic Syntax for declaration
Two dimensional array
if you want to crate n dimensional the the square brackets can be n.
Example:-
int[][] numbers=new numbers[10][10];
This will create an array named numbers with 10*10 matrix.


















0 comments:
Post a Comment
Post a Comment