在Java中实现一个新的LinkedList方法 [英] Implementing a new LinkedList method in Java

查看:131
本文介绍了在Java中实现一个新的LinkedList方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个赋值,涉及创建三个方法来处理链表。说明指示我使用以下构造函数:

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

但是,Eclipse似乎不喜欢我的代码,它。这是我目前的尝试:

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



现在我认为上面的MyList构造函数返回输入的列表,但我的Java技能真的很弱。我已经经历了教程,并给了这一个去,但它没有工作,因为我认为它会。

无论如何,所以Eclipse给我两个错误 - 在list = MyLIst(list);它说的方法MyList是未定义的ListClass,在public MyList行,它说方法的返回类型丢失 - 但我已经告诉它,r是一个链表,并返回。



这伤害了我的大脑,我无法设法弄清楚,任何人都可以给我一只手?我想如果我能够得到上面的代码工作,我应该能够得到其余的排序。



更新的代码



正如指出的,我的类名不同于我假设的构造函数名。所以这里是调整的代码:

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

这解决了return type错误

解决方案

使用修改后的代码,还是有一些东西更正:


  1. 在Java中,您可以调用构造函数来创建新的对象。当你创建它作为 main()方法的一部分时,你可能想保留这个对象,使用类似下面的方法来防止你的'undefined'错误: p>

      MyList ml = new MyList(list); 


  2. 作为构造函数的一部分,您只需存储 LinkedList< Integer> ; 作为局部变量传递,而不是类变量。请在类的顶部使用以下声明更正此错误:

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







其他功能的结构



为了添加下面注释中描述的附加功能,我将使用以下(显然你仍然需要实现方法,但你可以看到我把它放在哪里):

  import java.util.LinkedList; 

public class MyList {
private LinkedList< Integer>列表;

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

public LinkedList< Integer> reverse(){
//创建一个反向列表

return rList;
}

public LinkedList< Integer> odd(){
//创建奇数元素的列表

return oddList
}

public LinkedList< Integer> even(){
//创建偶数元素的列表

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


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.

解决方案

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

这篇关于在Java中实现一个新的LinkedList方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆