Implementing a new LinkedList method in Java

那年仲夏 提交于 2019-12-12 01:37:44

问题


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. 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. 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);" 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.

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.


回答1:


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. 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:

    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. 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);
    }
} 



回答2:


What's missing in the declaration of your method MyList is the return type of the method:

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).




回答3:


If

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

is supposed to be a Constructor, the class also must be named 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.

To get rid of the undefined problem, you need to create your list using 'new':

MyList myList  = new MyList(list);



回答4:


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.

So, either name both your class and the constructor MyList or name them 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:

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

or

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


来源:https://stackoverflow.com/questions/12056784/implementing-a-new-linkedlist-method-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!