• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to use enhanced loop

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How am I going to use enhanced for loop to dislay the content stored in the list l ? The way I used below does not work, why?
List <int[]> l = Arrays.asList( new int[] {1,2,3,4} );

for( Object o: l)
System.out.println(o);

Thanks
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
for( int[] o: l)
for(int i : o)
System.out.println(i);

Some more examples

ArrayList<string> array_list = new ArrayList<string> ();
array_list.add ("Abc");
array_list.add ("Def");
array_list.add ("Ghi");

for (String str : array_list)
System.out.println (str);
 
Ashley Bideau
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried HashMap for loop? Check below program.

import java.util.HashMap;
import java.util.Map;

/**
* Sample code to TEST HashMap. requires JDK 1.5+
*
*/
public class TestHashMap {
// --------------------------- main() method ---------------------------

/**
* Sample code to TEST HashMap. requires JDK 1.5+
*
* @param args not used
*/
public static void main( String[] args )
{
// create a new HashMap
HashMap<String, String> h = new HashMap<String, String>( 149
/* capacity */,
0.75f
/* loadfactor */ );

// add some key/value pairs to the HashMap
h.put( "WA", "Washington" );
h.put( "NY", "New York" );
h.put( "RI", "Rhode Island" );
h.put( "BC", "British Columbia" );

// look up a key in the HashMap
String stateName = h.get( "NY" );

// prints "New York"
System.out.println( stateName );

System.out.println( "enumerate all the keys in the HashMap" );
for ( String key : h.keySet() )
{
String value = h.get( key );

// prints lines of the form NY New York
// in effectively random order
System.out.println( key + " " + value );
}

System.out.println( "enumerate all the values in the HashMap" );
for ( String value : h.values() )
{
// prints lines of the form New York
// in effectively random order.
System.out.println( value );
}

System.out
.println( "enumerate all the key/value Entries in the HashMap" );
for ( Map.Entry<String, String> entry : h.entrySet() )
{
// prints lines of the form NY=New York
// in effectively random order.
System.out.println( "as Entry: " + entry );

// this does not require an expensive get lookup to find the value.
String key = entry.getKey();
String value = entry.getValue();
System.out.println( "separately: " + key + " " + value );
}
} // end main
}

[ April 30, 2006: Message edited by: Ashley Bideau ]
[ April 30, 2006: Message edited by: Ashley Bideau ]
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List <int[]> l = Arrays.asList( new int[] {1,2,3,4} );
Using this loop , displays the content is not 1, 2, 3, 4. It's the address.
for( int[] o: l)
System.out.println(i);

The following code does not compile: cannot convert from element typei int[] to int.
for(int i : o)
System.out.println(i);
 
Ashley Bideau
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should compile for more clarity use this version with {}

for( int[] o: l)
{
for(int i : o)
{
System.out.println(i);
}
}
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great! Got it.

Thanks
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic