For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
4
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
Ajaccio
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
3
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
row 0 and column 3
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
Vancouver
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
for ( int j = 0; j < cities[1].length; j++ )
System.out.println( cities[1][j] );
Munich
Stuttgart
Berlin
Bonn
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
for ( int i = 0; i < cities.length; i++ )
System.out.println( cities[i][1] );
LA
Stuttgart
Ajaccio
Ottawa
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
System.out.print( cities[i][j] + “\t” );
System.out.println( );
}
New York LA San Francisco Chicago
Munich Stuttgart Berlin Bonn
Paris Ajaccio Lyon
Montreal Ottawa Vancouver
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
{
if ( cities[i][j].length( ) == 6 )
System.out.println( cities[i][j] );
}
}
Munich
Berlin
Ottawa
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
int count = 0;
for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
{
if ( cities[i][j].length( ) == 7 )
count++;
}
}
System.out.println( “count is “ + count );
count is 2
For Questions 14 to 24, consider the following two-dimensional array declaration and initialization:
String [ ][ ] cities = {
{ “New York”, “LA”, “San Francisco”, “Chicago”}, { “Munich”, “Stuttgart”, “Berlin”, “Bonn” }, { “Paris”, “Ajaccio”, “Lyon” }, { “Montreal”, “Ottawa”, “Vancouver” } };
for ( int i = 0; i < cities.length; i++ )
{
for ( int j = 0; j < cities[i].length; j++ )
{
if ( cities[i][j].charAt( 0 ) == ‘S’ )
System.out.println( cities[i][j] );
}
}
San Francisco
Stuttgart
public static int foo( double [ ][ ] a )
{
int b = 0;
for ( int i = 0; i < a.length; i++ )
{
for ( int j = 0; j < a[i].length; j++ )
b++;
}
return b;
}. It counts and returns the number of elements in the parameter array a
public static boolean foo( char [ ][ ] a )
{
int b = a[0].length;
for ( int i = 1; i < a.length; i++ )
{
if ( a[i].length != b )
return false;
}
return true;
}It returns true if all the rows of the argument array a have the same number of
columns; otherwise, it returns false
public static int foo( String [ ][ ] a )
{
int b = 0;
for ( int i = 0; i < a.length; i++ )
b++;
return b;
}. It returns the number of rows in the argument array a
public static int [ ] foo( float [ ][ ] a )
{
int [ ] temp = new int [a.length];
for ( int i = 0; i < a.length; i++ )
temp[i] = a[i].length;
return temp;
}
It returns an int array of the same length as the length of the array parameter a.
Each element of the returned array stores the number of columns of the
corresponding row in a.
public static int foo( ArrayList a )
{
int b = 0;
for ( Integer i : a )
b++;
return b;
}It returns the number of elements in the ArrayList argument a
ArrayList a = new ArrayList( );
a. add( 7 );
a. add( 4 );
a. add( 21 );
7 (at index 0) 4 (at index 1) 21 (at index 2)
ArrayList a = new ArrayList( );
a. add( 7 );
a. add( 4 );
a. add( 21 );
a. set( 1, 45 );
7 (at index 0) 45 (at index 1) 21 (at index 2)
ArrayList a = new ArrayList( );
a. add( 7 );
a. add( 4 );
a. add( 21 );
a. add( 1, 45 );
7 (at index 0) 45 (at index 1) 4 (at index 2) 21 (at index 3)