String arrayName[] = new String[10];--> size of string array is 10.
or
String[] arrayName = new String[10];
where arrayName[0] = "item1";
arrayName[1] = "item2";
and so on until
arrayName[9] --> the last index is 9 because it starts with 0, ie, the last index is size of the array minus one.
array starts with zero not one.
We need to assign a new instance to the array to change the size.
String[] arrayName = new String[10];
arrayName[0] = "item1";
System.out.println(arrayName[0]); --> will return "item1".
arrayName = new String[20];
System.out.println(arrayName[0]); --> will return null.
String[] arrayName1 = new String[20];
arrayName1[0] = "item1";
System.out.println(arrayName1[0]); --> will return "item1".
String arrayName[] = new String[4];
arrayName[0] = "item1";
arrayName[1] = "item2";
arrayName[2] = "item3";
arrayName[3] = "item4";
Will throw Exception: class java.lang.ArrayIndexOutOfBoundsException: when the index is outside the index such as arrayName[-1] or arrayName[4]
array can be declared with actual value, such as :
String[] arrayName = {"item1", "item2","item3", "item4"}; --> this is size 4 array.
or this can be written as :
String[] arrayName = new String[] {"item1", "item2","item3", "item4"};
String[] arrayName = {"item1", "item2","item3", "item4"};
System.out.println( arrayName[0] );
System.out.println( arrayName[1] );
System.out.println( arrayName[2] );
System.out.println( arrayName[3] );
Answer will be :
item1
item2
item3
item4
String[] arrayName = {"item1", "item2","item3", "item4"};
System.out.println( arrayName[2] ); --> item3
System.out.println( arrayName[3] ); --> item4
System.out.println(arrayName.length ); -->4
String[] arrayName = new String[]{"obj1", "obj2","obj3"};
System.out.println( arrayName[2] ); --> obj3
System.out.println( arrayName[3] ); --> Will throw Exception: class java.lang.ArrayIndexOutOfBoundsException:
System.out.println(arrayName.length ); -->3
To iterate through the String Array :
String[] arrayName = new String[]{"obj1", "obj2","obj3"};
for( int i = 0; i < arrayName.length; i++)
{
System.out.println( arrayName[i] );
}
Answer;
obj1
obj2
obj3
//OR//
for( int i = 0; i < arrayName.length; i++)
{
String obj = arrayName[i];
System.out.println( obj );
}
Answer;
obj1
obj2
obj3
This iteration can be done in a different way also :
String[] arrayName = new String[]{"obj1", "obj2","obj3"};
for( String obj:arrayName)
{
System.out.println( obj);
}
Answer;
obj1
obj2
obj3
To check the presence of a value in the String Array :
String[] arrayName = new String[]{"obj1", "obj2","obj3"};
String expectedValue = "obj2";
boolean found = false;
for (String obj:arrayName) {
if ( obj.equals( expectedValue )) {
found = true;
}
}
if (found) {
System.out.println( "The array contains the value: " + expectedValue );
} else {
System.out.println( "The array does not contains the value: ");
}
Answer:
The array contains the value: obj2
Putting a break point to stop checking further when the searched value is found :
String[] arrayName= {"item1", "item2", "item3"};
String expectedValue = "item2";
boolean found = false;
for (String item:arrayName) {
if ( item.equals( expectedValue )) {
found = true;
break;
}
}
if (found) {
System.out.println( "The array contains the value: " + expectedValue );
} else {
System.out.println( "The array does not contains the value: " );
}
Answer:
The array contains the value: item2
Add an extra item to existing string array:
String[] arrayName = {"item1", "item2", "item3"};
String[] newArray = new String[ arrayName.length + 1 ];
System.out.println(newArray.length); // --> 4
newArray[arrayName.length] = "item4";
for(int j =0; j
System.out.println(newArray[j]);
}
Answer:
4
item1
item2
item3
item4
//OR//
String[] arrayName = {"item1", "item2", "item3"};
String[] newArray = new String[ arrayName.length + 1 ];
System.out.println(newArray.length); // --> 4
for (int i=0; i<arrayName.length; i++)
{
newArray[i] = arrayName[i];
}
newArray[arrayName.length] = "item4";
for(int j =0; j<newArray.length; j++)
{
System.out.println(newArray[j]);
}
Answer:
4
item1
item2
item3
item4
No comments:
Post a Comment