Results 1 to 3 of 3
  1. #1
    Werdeyo is offline Member
    Join Date
    Jan 2017
    Posts
    6
    Rep Power
    0

    Default For Each Loop help?

    Hey everyone I'm having a hard time understanding how the for each loop works like how when you use the variable item
    it gives you the key but when you use get(item) you get the value if someone could explain this in a simpler way it would
    be really appreciated. Thanks.

    Java Code:
    import java.util.HashMap;
    
    public class RestaurantForEach {
    	public static void main(String[] args) {
    
    		HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
    
    		restaurantMenu.put("Turkey Burger", 13);
    		restaurantMenu.put("Naan Pizza", 11);
    		restaurantMenu.put("Cranberry Kale Salad", 10);
    
    		System.out.println( restaurantMenu.size() );
    
    		for (String item : restaurantMenu.keySet()) {
    
    			System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");
    
    		}
    
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: For Each Loop help?

    Map's store (key, value) pairs. For a given key, you would use mapName.get(key) to retrieve the associated value. Think of an array. To
    get the value at index 10 of an array called vals you would use vals[10]. For an ArrayList named list you would use list.get(10). The same
    is true for Maps except that maps are not sequential access lists. The nice thing about maps is that you are not restricted to using only integers to access values. In your case above you are using Strings to get access to the associated integer.

    Edit: I used the wrong term here. Arrays and array Lists are not sequential access data structures but random access structures (compared to a linked list which is accessed sequentially). But then a Map is also a random access data structure. So the best way to really describe them is that that arrays and array Lists are organized linearly where as maps are not. I hope I haven't confused you too much.

    Regards,
    Jim
    Last edited by jim829; 01-09-2017 at 01:48 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: For Each Loop help?

    The real problem is that 10 dolllars for a cranberry kale salad is way overpriced :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 10:27 AM
  2. Replies: 7
    Last Post: 04-03-2013, 08:26 PM
  3. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  4. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  5. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •