Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

implementing iterator for linked list java

public class BasicLinkedList<T> implements Iterable<T> {
    public int size;

    private class Node {
        private T data;
        private Node next;

        private Node(T data) {
            this.data = data;
            next = null;
        }
    }

    private Node head;
    private Node tail;

    public BasicLinkedList() {
        head = tail = null;
    }
//Add, remove method 

public Iterator<T> iterator() {
        return new Iterator<T>() {

            Node current = head;

            @Override
            public boolean hasNext() {
                return current != null;
            }

            @Override
            public T next() {
                if(hasNext()){
                    T data = current.data;
                    current = current.next;
                    return data;
                }
                return null;
            }

            @Override
            public void remove(){
                throw new UnsupportedOperationException("Remove not implemented.");
            }

        };
Comment

iterate trough linked list java

#Enhances For Loop
for (String temp : linkedList) {
    System.out.println(temp);
}
Comment

Java Iterating through LinkedList

import java.util.LinkedList;

class Main {
    public static void main(String[] args) {
        // Creating a linked list
        LinkedList<String> animals = new LinkedList<>();
        animals.add("Cow");
        animals.add("Cat");
        animals.add("Dog");
        System.out.println("LinkedList: " + animals);

        // Using forEach loop
        System.out.println("Accessing linked list elements:");
        for(String animal: animals) {
            System.out.print(animal);
            System.out.print(", ");
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java Create LinkedList in Java 
Java :: running sum of 1d array java 
Java :: android xml hide 
Java :: volley dependency 
Java :: How to initialize a 2d array in Java? 
Java :: android get distance between two locations kotlin 
Java :: how to declare and insert value to arraylist in same line in java 
Java :: validate date java 
Java :: java secretkey 
Java :: generatedvalue spring boot 
Java :: array java 
Java :: java producer consumer 
Java :: discord jda 
Java :: sort an int array java 
Java :: declare array of chars java 
Java :: long vs int java 
Java :: List into string java 
Java :: javafx start 
Java :: springboot mongodb test 
Java :: formatting an integer in java 
Java :: how to run individual test in java maven 
Java :: sum of array elements java 
Java :: list of numbers java 
Java :: java sort int array 
Java :: how to find the prime numbers in java 
Java :: Maven Dependency for flyway 
Java :: calling activity method from fragment: 
Java :: java exception list 
Java :: reviews button ade android studio 
Java :: calculate smallest angle difference 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =