简体   繁体   English

想要从第一个到最后一个元素。 但是返回到第一个

[英]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. 在get方法中,我想要从第一个元素到最后一个元素。 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? 看起来您在问为什么.get()不返回预期的元素?

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. 当引用在列表中向前移动时,循环计数器将从size()递减到所需的索引。 This means that the value of size bears no real resemblance to what index you're looking at. 这意味着size的值与您要查看的索引没有真正的相似之处。

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 . 基本的问题是您的get(i)方法没有返回位置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. 然后,不必费心编写自己的List类。 Java has several perfectly good List implementations. Java有几个非常好的List实现。

Example: 例:

import java.util.*; 导入java.util。*; class DemoList { 类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);
    }

}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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