简体   繁体   中英

Can you store objects of the same type to another class without using the java inbuilt array, list, etc.?

I am trying to do my own version of a linked list without using any Java inbuilt methods. I already got the nodes part down where every element is connected to another, but I can't think how I would be able to store all nodes into one class.

I thought about how I would traverse the whole list forward or backward just like an iterator would, but I have no idea how to store objects (not a fixed amount. They would need to be stored just how list are dynamic and they change based on the number of inputs we specify, or at a later date to add a new object and the size to change automatically). I thought about it and all day and searched on Google, but I wasn't able to find what I am writing here. Do you have any ideas?

public class ListItem {
    private String value;
    private ListItem nextItem;
    private ListItem previousItem;
    public ListItem(String value) {
        this.value = value;
    }

    public void setNextItem(ListItem nextItem) {
        this.nextItem = nextItem;
    }

    public void setPreviousItem(ListItem previousItem) {
        this.previousItem = previousItem;
    }


    public String getValue() {
        return value;
    }

    public ListItem getNextItem() {
        return nextItem;
    }

    public ListItem getPreviousItem() {
        return previousItem;
    }
}

This is the base Node class, and now I am working on the LinkedList class.

public class ListItem {

private String value;
private ListItem nextItem;
private ListItem previousItem;

public ListItem(String value) {
    this.value = value;
}

public void setNextItem(ListItem nextItem) {
    this.nextItem = nextItem;
}

public void setPreviousItem(ListItem previousItem) {
    this.previousItem = previousItem;
}


public String getValue() {
    return value;
}

public ListItem getNextItem() {
    return nextItem;
}

public ListItem getPreviousItem() {
    return previousItem;
}

}

This is the base Node class and now i am working on the LinkedList class.

public class MyLinkedList {

private ListItem head;
private ListItem currentItem;

public void addToList(String value){
    ListItem item=new ListItem(value);
    if(head == null){
        head = item;
    } else {
        ListItem tail = head;

        while (tail.getNextItem() != null){
            tail = tail.getNextItem();
            int comparator=tail.getValue().compareTo(item.getValue());
            if(comparator > 0){ // tail > item
                if(tail.getNextItem() == null){
                    tail.setNextItem(item);
                    item.setPreviousItem(tail);
                   return;
                }
                continue;

            } else if (comparator < 0) { // tail < item
               item.setPreviousItem(tail.getPreviousItem());
                tail.getPreviousItem().setNextItem(item);
               item.setNextItem(tail);
                tail.setPreviousItem(item);
            }
        }
    
    }
    setCurrItem();
}

public void setCurrItem(){
    currentItem=head;
}
 

public void moveNext(){
    ListItem next=currentItem.getNextItem();
    if(next == null){
        System.out.println("You have reached the end of the list");
    } else {
        currentItem=next;
    }

}

public void movePrevious(){
    ListItem prev=currentItem.getPreviousItem();
    if(prev == null){
        System.out.println("You have reached the start of the list");
    } else {
        currentItem=prev;
    }

}

public ListItem getCurrentItem() {
    return currentItem;
}

}

I managed to make it work based on the help that I got, now I am trying to order the list alphabetically using .compareTo() method the problem is the code doesn't work, first few times I've gotten NullPointerException now i made it to work but it only registers the first node of the list meaning it doesn't do anything in the while loop from the method addToList(), all that I tried to do in that while is set new references between the node(tail) and the new item where needed. If somebody wants to run it to see the problem I put below the main code:

public class Main {

private static Scanner scanner=new Scanner(System.in);
private static MyLinkedList linkedList = new MyLinkedList();
public static void main(String[] args) {

    linkedList.addToList("Bob");
    linkedList.addToList("John");
    linkedList.addToList("Kim");
    linkedList.addToList("Jimmy");
    linkedList.addToList("Paul");


    boolean start = true;
    printOptions();

    while (start) {
        System.out.println("Current value of the element is " + linkedList.getCurrentItem().getValue());
        int choice= scanner.nextInt();
        scanner.nextLine();
        switch (choice){
            case 0:
                System.out.println("Exiting ...");
                start=false;
                break;
            case 1:
                linkedList.moveNext();
                break;
            case 2:
                linkedList.movePrevious();
                break;
            case 3:
                printOptions();
                break;
        }
    }
}

private static void printOptions(){
    System.out.println("\nPress");
    System.out.println("\t0 - to exit");
    System.out.println("\t1 - to move forward");
    System.out.println("\t2 - to move backwards");
    System.out.println("\t3 - to print the options");
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM