简体   繁体   English

在Java中实现新的LinkedList方法

[英]Implementing a new LinkedList method in Java

I have an assignment that involves creating three methods that manipulate a linked list. 我的作业涉及创建三种操作链表的方法。 The instructions dictate that I use the following constructor: 这些说明指示我使用以下构造函数:

 public MyList (LinkedList<Integer> list) {
 ...
 }

However, Eclipse seems to not like my code regardless of how I try integrate it. 但是,无论我如何尝试将其集成,Eclipse似乎都不喜欢我的代码。 Here's my current attempt: 这是我目前的尝试:

import java.util.*;

public class ListClass {

public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();
    list.add(10);
    list = MyList(list);
}

public MyList (LinkedList<Integer> list){
    LinkedList<Integer> r = list;
    return r;
}
}

Now I thought that the MyList constructor above would happily just return the list entered, but my Java skills are really weak. 现在我认为上面的MyList构造函数会很乐意返回输入的列表,但是我的Java技能确实很弱。 I've been going through the tutorials and gave this a go, but it hasn't worked as I thought it would. 我一直在仔细阅读教程,并尝试了一下,但是它没有达到我的预期。

Anyway so Eclipse is giving me two errors - at the "list = MyLIst(list);" 无论如何,Eclipse给了我两个错误-在“ list = MyLIst(list);”处 line it says the method MyList is undefined for ListClass, and at the "public MyList" line it says "the return type for the method is missing" - but I've told it that r is a linked list, and to return that. 该行表示对于ListClass未定义方法MyList,在“ public MyList”行中表示“该方法的返回类型丢失”-但我告诉过它r是一个链表,然后将其返回。

This hurts my brain and I can't manage to figure it out, can anyone give me a hand? 这伤了我的大脑,我无法弄清楚,有人可以帮我吗? I think if I were able to get the above code working, I should be able to get the rest sorted. 我认为,如果我能够使上述代码正常工作,那么我应该就能对其余的代码进行排序。

Newer code 较新的代码

As rightfully pointed out, my class name isn't the same as my supposed constructor name. 正确地指出,我的类名与假定的构造函数名不同。 So here's the adjusted code: 因此,这是调整后的代码:

import java.util.LinkedList;

public class MyList {

public static void main(String[] args) {
    LinkedList<Integer> list = new LinkedList<Integer>();
    list.add(10);
    list.add(-20);
    MyList(list);
}

public MyList(LinkedList<Integer> list) {
    LinkedList<Integer> newList = list;
    System.out.println(newList);    
}
}

This has solved the "return type" error (thank you), though I'm still getting the "undefined" error. 尽管我仍然遇到“未定义”错误,这已经解决了“返回类型”错误(谢谢)。

What's missing in the declaration of your method MyList is the return type of the method: 方法MyList的声明中缺少的是方法的返回类型:

public MyList (LinkedList<Integer> list)

should be something like 应该是这样的

public LinkedList<Integer> MyList (LinkedList<Integer> list)

Besides that, the usual convention for method names is camel case, but starting with a lower-case letter. 除此之外,方法名称的常规约定为驼峰式,但以小写字母开头。 I'd call it myList instead of MyList (you should choose a better name for the method that reflects what the purpose of the method is). 我把它叫做myList而不是MyList (你应该选择反映了该方法的目的是什么方法更好的名字)。

If 如果

public MyList (LinkedList<Integer> list) {
 ...
}

is supposed to be a Constructor , the class also must be named MyList . 如果应该是一个构造器 ,该类也必须命名为MyList You can't return anything from a constructor, so just leave the declaration of it as it is. 您不能从构造函数返回任何内容,因此只需保留其声明即可。

Just rename your class, save the LinkedList to a private field in the constructor above, and then add the methods you are supposed to implement to the MyList class. 只需重命名您的类,将LinkedList保存到上面的构造函数中的私有字段中,然后将应该实现的方法添加到MyList类中即可。

To get rid of the undefined problem, you need to create your list using 'new': 要摆脱未定义的问题,您需要使用“ new”创建列表:

MyList myList  = new MyList(list);

The problem here is that a constructor must have the same name of its enclosing class. 这里的问题是,构造函数必须具有与其所在的类相同的名称。 However, you're trying to name a MyList constructor inside a class named ListClass. 但是,您试图在名为ListClass的类中命名MyList构造函数。

So, either name both your class and the constructor MyList or name them ListClass. 因此,可以同时命名您的类和构造函数MyList或将它们命名为ListClass。

As for the "undefined" issue, you can't directly call a constructor. 至于“未定义”问题,您不能直接调用构造函数。 You have to use it in a "new" statement, as it is used to create new instances of the class: 您必须在“ new”语句中使用它,因为它用于创建类的新实例:

MyList someList = new MyList(); // variable someList will hold a new MyList instance

or 要么

new MyList(); // instance without a reference variable.

With your modified code, there's still a few things to correct: 使用修改后的代码,还有一些要纠正的地方:

  1. In Java, you call a constructor in order to create a new Object . 在Java中,您调用构造函数以创建新的Object You probably want to keep this object when you create it as part of your main() method, using something like the following in order to prevent your 'undefined' error: 在创建对象作为main()方法的一部分时,您可能希望保留该对象,并使用如下所示的内容来防止“未定义”错误:

     MyList ml = new MyList(list); 
  2. As part of your Constructor you only store the LinkedList<Integer> that's passed in as as local variable, and not as a class variable. 作为构造函数的一部分,您只存储作为本地变量而不是类变量传入的LinkedList<Integer> Correct this with the following declaration at the top of your class: 使用班级顶部的以下声明进行更正:

     public class MyList { private LinkedList<Integer> list; //... 

Structure for additional functionality 附加功能的结构

In order to add the additional functionality as described in your comment below, I'd use the following sort of structure (Obviously you still need to implement the methods, but you can see where I'd put them): 为了添加下面的注释中所述的其他功能,我将使用以下类型的结构(显然,您仍然需要实现这些方法,但是您可以看到它们的放置位置):

import java.util.LinkedList;

public class MyList {
    private LinkedList<Integer> list;

    public MyList(LinkedList<Integer> list) {
        this.list = list;
    }

    public LinkedList<Integer> reverse() {
        //Create a reversed list

        return rList;
    }

    public LinkedList<Integer> odd() {
        //Create a list of the odd elements

        return oddList
    }

    public LinkedList<Integer> even() {
        //Create a list of the even elements

        return evenList;
    }

    @Override
    public String toString() {
        return list.toString();
    }

    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<Integer>();
        list.add(0);
        list.add(2);
        list.add(4);

        MyList ml = new MyList(list);
        System.out.println("MyList: " + ml);
        LinkedList<Integer> tsil = ml.reverse();
        System.out.println("Reversed: " + tsil);
        LinkedList<Integer> ls = ml.odd();
        System.out.println("Odd: " + ls);
        LinkedList<Integer> it = ml.even();
        System.out.println("Even: " + it);
    }
} 

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

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