double [ ] a = { 12.5, 48.3, 65.0 };
System.out.println( a[1] );
48.3
int [ ] a = new int [6];
System.out.println( a[4] )
0
double [ ] a = { 12.5, 48.3, 65.0 };
System.out.println( a.length );
3
int [ ] a = { 12, 48, 65 };
for ( int i = 0; i < a.length; i++ )
System.out.println( a[i] );
Output of code:
12
48
65
int [ ] a = { 12, 48, 65 };
for ( int i = 0; i < a.length; i++ )
System.out.println( “a[” + i + “] = “ + a[i] );
Output of code:
a[0] = 12
a[1] = 48
a[2] = 65
int s = 0;
int [ ] a = { 12, 48, 65 };
for ( int i = 0; i < a.length; i++ )
s += a[i];
System.out.println( “s = “ + s );
Output of code:
s = 125
int [ ] a = new int[10];
for ( int i = 0; i < a.length; i++ )
a[i] = i + 10;
System.out.println( a[4] );
Output of code:
14
double [ ] a = { 12.3, 99.6, 48.2, 65.8 };
double temp = a[0];
for ( int i = 1; i < a.length; i++ )
{
if ( a[i] > temp )
temp = a[i];
}
System.out.println( temp );Output of code:
99.6
int [ ] a = { 12, 48, 65, 23 };
int temp = a[1];
a[1] = a[3];
a[3] = temp;
for ( int i = 0; i < a.length; i++ )
System.out.print( a[i] + “ “ );
Output of code: 12 23 65 48
public int foo( int [ ] a )
{
int temp = 0;
for ( int i = 0; i < a.length; i++ )
{
if ( a[i] == 5 )
temp++;
}
return temp;
}It counts how many elements in the argument array have the value 5
public int foo( int [ ] a )
{
for ( int i = 0; i < a.length; i++ )
{
if ( a[i] == 10 )
return i;
}
return -1;
}It returns the index of the first element in the argument array with value 10. If no
element in the argument array has value 10, it returns –1.
public boolean foo( int [ ] a )
{
for ( int i = 0; i < a.length; i++ )
{
if ( a[i] < 0 )
return false;
}
return true;
}If the argument array has at least one element that is negative, it returns false, otherwise it returns
true.
public String [ ] foo( String [ ] a )
{
String [ ] temp = new String[a.length];
for ( int i = 0; i < a.length; i++ )
{
temp[i] = a[i].toLowerCase( );
}
return temp;
}
It returns an array of Strings identical to the argument array except that the Strings
are all in lowercase.
public boolean [ ] foo( String [ ] a )
{
boolean [ ] temp = new boolean[a.length];
for ( int i = 0; i < a.length; i++ )
{
if (a[i].indexOf( "@" ) != -1 )
temp[i] = true;
else
temp[i] = false;
}
return temp;
}It returns an array of booleans as follows: if an element in the argument array
contains the character @, then the corresponding element in the returned array is
set to true, otherwise it is set to false.