Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java question: Can the enhanced for loop loop through linked list structures?
New Posts  All Forums:Forum Nav:

Java question: Can the enhanced for loop loop through linked list structures?

post #1 of 5
Thread Starter 
http://introcs.cs.princeton.edu/java/43stack/Stack.java.html

public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}


How exactly is the enhanced for loop going through the stack?
New and Shiny
(18 items)
 
  
CPUMotherboardGraphicsRAM
Rynze 7 1700 ASUS ROG Crosshair VI Hero Sapphire RX Vega 56 G.SKILL TridentZ F4-3200C14D 
Hard DriveHard DriveHard DriveHard Drive
Kingston HyperX 3K Crucial MX300 Western Digital Black Western Digital Green 
Hard DriveCoolingOSMonitor
Western Digital Red Noctua NH-U14S Windows 10 Pro Dell U2414H 
MonitorKeyboardPowerCase
Dell P2414H Ducky One Corsair RM650x NZXT H440 White 
MouseAudio
Logitech G502 Proteus Spectrum Schiit Modi2U & Magni2U 
  hide details  
Reply
New and Shiny
(18 items)
 
  
CPUMotherboardGraphicsRAM
Rynze 7 1700 ASUS ROG Crosshair VI Hero Sapphire RX Vega 56 G.SKILL TridentZ F4-3200C14D 
Hard DriveHard DriveHard DriveHard Drive
Kingston HyperX 3K Crucial MX300 Western Digital Black Western Digital Green 
Hard DriveCoolingOSMonitor
Western Digital Red Noctua NH-U14S Windows 10 Pro Dell U2414H 
MonitorKeyboardPowerCase
Dell P2414H Ducky One Corsair RM650x NZXT H440 White 
MouseAudio
Logitech G502 Proteus Spectrum Schiit Modi2U & Magni2U 
  hide details  
Reply
post #2 of 5
I'm not really sure what you're asking there as you've acknowledged it's an enhanced loop rather than a typical, iterated, for loop. But if you're asking what enhanced loops are then Wikipedia gives a simple explanation plus oodles of examples: https://en.wikipedia.org/wiki/Foreach_loop

But to paraphrase, the point of enhanced loops (or for each loops, as most other languages call them) is that you specify the collection and the language manages the iterators at compile time rather than expecting developers to code those syntax manually. For each loops also have benefits as they can cleanly cycle through unordered lists like maps / hashes / named arrays (depending on the language in question) which a typical for (i = 0; i < collection; i++) couldn't.

Hopefully that answers your question, but I suspect I've given a too basic overview and basically told you what you already knew?
post #3 of 5
Quote:
Originally Posted by Plan9 View Post

I'm not really sure what you're asking there as you've acknowledged it's an enhanced loop rather than a typical, iterated, for loop. But if you're asking what enhanced loops are then Wikipedia gives a simple explanation plus oodles of examples: https://en.wikipedia.org/wiki/Foreach_loop

But to paraphrase, the point of enhanced loops (or for each loops, as most other languages call them) is that you specify the collection and the language manages the iterators at compile time rather than expecting developers to code those syntax manually. For each loops also have benefits as they can cleanly cycle through unordered lists like maps / hashes / named arrays (depending on the language in question) which a typical for (i = 0; i < collection; i++) couldn't.

Hopefully that answers your question, but I suspect I've given a too basic overview and basically told you what you already knew?

That's pretty much it. If the object implements a specific interface (in C# its one of the IEnumerable derivatives, no clue what it is in Java) the compiler is able to generate code to iterate over the object by calling the methods on the interface (for example, in C# it first calls the IEnumerable GetEnumerator method of the object) to retrieve an IEnumerator object.

It then calls the IEnumerator.MoveNext while it continues to return true (i.e. there's still more to iterate over). IEnumerator contains one other property called Current which retrieves the current item.

Its basically the same in many other languages. C# is close to Java so I'll give a C# example:
Code:
int[] numbers = new int[] { 1, 2, 3 };

foreach ( int number in numbers )
{
        Console.WriteLine( number );
}

The compiler actually generates code similar to:
Code:
int[] numbers = new int[] { 1, 2, 3 };
IEnumerable enumerable = numbers as IEnumerable;

if ( enumerable != null )
{
        IEnumerator enumerator = enumerable.GetEnumerator();
        if ( enumerator != null )
        {
                while ( enumerator.MoveNext() )
                {
                        Console.WriteLine( enumerator.Current );
                }
        }
}


This works because the .NET framework standard array object implements IEnumerable:
Code:
public abstract class Array : ICloneable, IList, IStructuralComparable, IStructuralEquatable

It implements IEnumerable via proxy of IList:
Code:
public interface IList : ICollection, IEnumerable

I'm sure it is similar somehow in Java.

So yes, if your linked list object implements the required interfaces in Java then you can use the "enhanced" for loop.
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #4 of 5
Java's foreach loop works with any object that implements the java.lang.Iterable interface. This includes all Collection types (including Lists) in the standard Java collections library.
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
Cube
(9 items)
 
  
CPUMotherboardRAMHard Drive
i7-4930k EVGA X79 Dark Corsair Vengeance Pro Samsung 840 Pro 
CoolingMonitorPowerCase
Corsair H100i Viewsonic VP2770 EVGA SuperNova 1000P2 Corsair Air 540 
Mouse
Corsair M65 
  hide details  
Reply
post #5 of 5
Thread Starter 
Ah, I get it. Thanks guys.
New and Shiny
(18 items)
 
  
CPUMotherboardGraphicsRAM
Rynze 7 1700 ASUS ROG Crosshair VI Hero Sapphire RX Vega 56 G.SKILL TridentZ F4-3200C14D 
Hard DriveHard DriveHard DriveHard Drive
Kingston HyperX 3K Crucial MX300 Western Digital Black Western Digital Green 
Hard DriveCoolingOSMonitor
Western Digital Red Noctua NH-U14S Windows 10 Pro Dell U2414H 
MonitorKeyboardPowerCase
Dell P2414H Ducky One Corsair RM650x NZXT H440 White 
MouseAudio
Logitech G502 Proteus Spectrum Schiit Modi2U & Magni2U 
  hide details  
Reply
New and Shiny
(18 items)
 
  
CPUMotherboardGraphicsRAM
Rynze 7 1700 ASUS ROG Crosshair VI Hero Sapphire RX Vega 56 G.SKILL TridentZ F4-3200C14D 
Hard DriveHard DriveHard DriveHard Drive
Kingston HyperX 3K Crucial MX300 Western Digital Black Western Digital Green 
Hard DriveCoolingOSMonitor
Western Digital Red Noctua NH-U14S Windows 10 Pro Dell U2414H 
MonitorKeyboardPowerCase
Dell P2414H Ducky One Corsair RM650x NZXT H440 White 
MouseAudio
Logitech G502 Proteus Spectrum Schiit Modi2U & Magni2U 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java question: Can the enhanced for loop loop through linked list structures?