【问题标题】:Why is my list not changing为什么我的列表没有改变
【发布时间】:2013-06-04 12:18:39
【问题描述】:

所以对于家庭作业,我必须编写一个程序来创建一个名称的链表,使其循环,然后删除每个第 n 个节点,直到只剩下一个东西。我的代码打印出东西被取出但从未真正取出,有人能告诉我为什么吗?注意:因为这是家庭作业,所以我不是在寻找确切的答案,更多关于如何修复我的代码的有用建议

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Survivor {
    public static void main(String[] args){
        ListNode list=makeNode();
        ListNode t=list;
        Scanner sc=new Scanner(System.in);
        while(t.getNext()!=null){           
            System.out.print(t.getValue() + "; ");
            t=t.getNext();
        }
        System.out.println();
        System.out.print("Enter a number to kill: ");
        int kill=sc.nextInt();
        kill(kill,list);    
        t=list;
        while(t.getNext()!=null){           
            System.out.print(t.getValue() + "; ");
            t=t.getNext();
        }
    }

    public static ListNode makeNode(){
        ListNode names=null;
        File data=new File("names.txt");
        Scanner fl=null;
        ListNode t=names;
        try {
            fl=new Scanner(data);
        } catch (FileNotFoundException e) {
            //handle errors
            System.out.println("The File isnt there"+data.getAbsolutePath()+"\"");
            System.exit(-1);
        }
        while(fl.hasNext()){
            String theName=revName(fl.nextLine());
            if(t!=null){
                if(t.getNext()!=null){
                    ListNode t2=t.getNext();
                    if(theName.compareToIgnoreCase((String) names.getValue())<0){
                        names=(new ListNode(theName, names));                   
                    }
                    if(theName.compareToIgnoreCase((String) names.getValue())>0){               
                        while(t.getNext() !=null &&theName.compareToIgnoreCase((String) t2.getValue())>0 ){                     
                            t=t.getNext();
                            t2=t2.getNext();
                        }
                        t.setNext(new ListNode((theName),t2));
                    }
                }
                if(t.getNext()==null){
                    if(theName.compareToIgnoreCase(((String)t.getValue()))<0){
                        names=(new ListNode((theName),null));
                    }
                    else{
                        t.setNext(new ListNode(theName,null));
                    }
                }
                t=names;
            }
            if(names==null){
                names=new ListNode(theName,null);
                t=names;
            }
            t=names;
        }
        return(names);
    }

    public static String getCauseOfDeath(String name){
        int first=(int)(Math.random()*10);
        String cause = "fell off of a cliff";
        if(first==1)
            cause="tripped";
        if(first==2)
            cause="got a boo-boo and died";
        if(first==3)
            cause="insulted the gods";
        if(first==4)
            cause="had a coconut fall on their head";
        if(first==5)
            cause="drowned";
        if(first==6)
            cause="showed up to cuadrados class late";
        if(first==7)
            cause="tried to steal honey from giant bees and failed";
        if(first==8)
            cause="doesn't even lift";
        if(first==9)
            cause= "got bitten by a unicorn and poisoned";
        if(first==10)
            cause="got lost and starved";
        return("Alas, the mighty " + name+ " has fallen; " + name + " "+cause);
    }

    public static String revName(String name){
        String name1;
        String name2;
        int space;
        int dot;
        dot=name.lastIndexOf(".");
        if (dot == -1){
            name= name.trim(); //Trims spaces at front and end
            space=name.lastIndexOf(" ");

            if (space == -1){  
                //If there are no spaces in the code this line just returns the name
                return name;
            }
            else{
                name1=name.substring(0, space);
                name2=name.substring(space+1);
                if(name2.length()>1){
                    return name2+ " " + name1;
                }
                else{
                    space=name.indexOf(" ");
                    name1=name.substring(0, space);
                    name2=name.substring(space+1);
                    return name2+" "  + name1;
                }
            }
        }
        return (name);
    }

    public static void kill(int i, ListNode list){
        ListNode t=list;
        ListNode t2=t.getNext();
        int z=2;
        while(t2.getNext()!=null){          
            if(i==z){       
                System.out.println(getCauseOfDeath(revName((String) t2.getValue())));
                t.setNext(t2.getNext());
                z=0;
            }
            z++;
            t=t.getNext();
            t2=t2.getNext();
        }
    }
}

【问题讨论】:

  • 发布唯一重要的部分:ListNode.setNext()
  • 我在他的代码中看到的
  • @user2319595 是 C 语法,x-&gt;y 大致相当于 Java 中的 x.y
  • 你为什么总是比较你放在名单上的名字?

标签: java string list


【解决方案1】:

可能跟给kill方法传参数有关吧?

Is Java "pass-by-reference" or "pass-by-value"?

【讨论】:

  • 不这么认为,因为 setNext() 会破坏第 n 个元素
  • 我认为如果你编辑了列表它就不需要返回,我尝试让它返回但它没有
【解决方案2】:

您可能会考虑使用链表的remove 功能来删除元素。另外不要忘记Java确实通过引用来操作对象,并且所有对象变量都是引用。但是,Java 不会通过引用传递方法参数;它按值传递它们。看看here for more information on how things are passed in Java.

【讨论】:

  • 谢谢大家,它现在工作得很好:D,我在制作/打印列表时也遇到了一些错误,我也修复了!
猜你喜欢
  • 1970-01-01
  • 2022-12-17
  • 2018-11-21
  • 2016-01-03
  • 1970-01-01
  • 2018-01-25
  • 2020-01-13
  • 2021-10-31
  • 2022-12-06
相关资源
最近更新 更多