Array and String-Converted New
Array and String-Converted New
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure where we
store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element
is stored on 1st index and so on.
A list of items can be given one variable name using only one subscript such a variable is called
a single-scripted or one –dimensional array.
Example
int a[]=new int[5];
for this array computer reserve five memory locations as
Creating Array:
Array must be declared and created in computer memory before they are used. Creation of array
involves three steps:
1. Declare the array
2. Create memory locations
3. Put values into the locations
Declaration of Arrays
Array in java may be declared in two forms.
Form1
Datatype Arrayname[ ];
Form2
Datatype[ ] Arrayname;
Example:
int number[ ];
floar percentage []
int[ ] count;
float[ ] average;
Creation of Arrays
Two dimensional array requires two subscript one for Row and another for Column .Data in two
dimensional arrays are stored in tabular form (in row major order).
Declaration of 2D array:
int a[ ][ ];
a = new int [3][4];
or
int[][] a = new int[3][4];
Here, a is a two-dimensional (2d) array. The array can hold maximum of 12 elements of
type int.
Initialization of 2D array :
Ex.
int a[ ][ ] = {1,2,3,4,5,6,7,8,9,10,11,12};
Or
int a[ ][ ] = { {1,2,3,4}, {5,6,7,8}, {9,10,11,12} };
or
int a[ ][ ] = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};
Below code shows different ways for string array declaration in java.
String[ ] strArray; //declare without size
String Methods:
The String class has a set of built-in methods that you can use on strings.
Method Description Return Type
By
********************