为什么我的清单没有改变

时间:2013-06-04 12:18:39

标签: java string list

因此,对于作业,我必须编写一个程序,创建一个名称链接列表,使其成为循环,然后删除每个第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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

也许它与将参数传递给kill方法有关?

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

答案 1 :(得分:0)

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