简体   繁体   中英

Wants to get an element from 1st to last. But returns last to 1st

In get method, I want to get the element from first element to last. But it returns the list in reversed order (last to first element). How can I solve that problem with this code ?

import java.util.*;

class List {

    Customer listPtr;
    int index;

    public void add(Customer customer) {
        Customer temp = customer;
        if (listPtr == null) {
            listPtr = temp;
            index++;
        } else {
            Customer x = listPtr;
            while (x.next != null) {
                x = x.next;
            }
            x.next = temp;
            index++;
        }
    }

    public Customer get(int index) {
        Customer temp = listPtr;
        int size = size();
        if (index == 0) {
            return listPtr;
        } else {
            while (size != index) {
                size--;
                temp = temp.next;
                System.out.println(size + "------" + index);
            }
            return temp;
        }
    }

    public int size() {
        int size = 0;
        Customer temp = listPtr;
        while (temp != null) {
            temp = temp.next;
            size++;
        }
        return size;
    }

    public void printList() {
        Customer temp = listPtr;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }
}

class DemoList {

    public static void main(String args[]) {
        List list = new List();
        Customer c1 = new Customer("10011", "A");
        Customer c2 = new Customer("10012", "B");
        Customer c3 = new Customer("10013", "C");
        Customer c4 = new Customer("10014", "D");
        Customer c5 = new Customer("10015", "E");
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        System.out.println(list.get(1));
        //list.printList();

    }
}

class Customer {

    String id;
    String name;
    Customer next;

    public Customer(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return id + " : " + name;
    }

    public boolean equals(Object ob) {
        Customer c = (Customer) ob;
        return this.id.equals(c.id);
    }
}

the question is somewhat ambiguous. it looks like you're asking why .get() isn't returning the expected element?

while (size != index) {
    size--;
    temp = temp.next;
    System.out.println(size + "------" + index);
}
return temp;

appears to be the culprit. your loop counter is decrementing from size() to desired index while your reference is moving forward through the list. This means that the value of size bears no real resemblance to what index you're looking at.

I'd use

int i =0;
while(++i <= index && i < size){
    temp = temp.next;
}
if (i == index) {
   return temp
} else {
   //off the end, so throw exception
}

Since this is an assignment, it is not appropriate (or in your interest in the long-term) to give you a solution ...

The basic problem is that your get(i) method is not returning the value a position i .

That should be enough of a Hint to allow you to figure out what it is really doing ... and hence to fix it.

Looks like you are doing something very simple in a very complicated way.

I suggest:

First, remove the 'next' field from Customer.

Then, don't bother writing your own List class. Java has several perfectly good List implementations.

Example:

import java.util.*; class DemoList {

public static void main(String args[]) {
    List<Customer> list = new ArrayList<Customer>();
    Customer c1 = new Customer("10011", "A");
    Customer c2 = new Customer("10012", "B");
    Customer c3 = new Customer("10013", "C");
    Customer c4 = new Customer("10014", "D");
    Customer c5 = new Customer("10015", "E");
    list.add(c1);
    list.add(c2);
    list.add(c3);
    list.add(c4);

    //first customer:
    System.out.println(list.get(0)); //zero-based

    //all customers:
    for (Customer c : list) {
       System.out.println(c);
    }

}

}

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