1

嘿伙计们,我正在尝试通过将前 3 个字母相乘来按字母顺序对链接列表进行排序。它的工作方式是第一个字母取 26^2,第二个字母取 26^1,第三个取 26^0。当我运行程序时,它给了我相同的金额来说出名字“lala”和“francis”。如果有人能帮我看看代码有什么问题,将不胜感激!

LinkedListNode 类:(包含 getSum 方法)

public class LinkedListNode 
{ 

    public String data; 
    public LinkedListNode next; 
   public long sum; 


    public LinkedListNode(String data)  
   { 
        this.data = data; 
        this.next = null; 
        this.sum = getSum(data); 
    }//end node 

   public long getSum(String line) 
    { 
        int i; 
        long sum = 0; 
        String s = null; 
      char a; 

           for(i=0; i < 3; i++) 
         { 
            int j = 2; 
            a = line.charAt(i);      
            sum += Character.getNumericValue(a) * Math.pow(26, j); 
            //Return the value of the number 4 to be the power of 3 (4*4*4): Math.pow(4,3); 
            j--; 
           }//end for  

        return sum; 
    }//end getSum 

    public long getSum() 
    { 
      return sum; 
    }//end getSum 

    public String getData()  
   { 
        return data; 
    }//end getData 
    public void setData(String data)  
   { 
        this.data = data; 
    }//end setData 
    public LinkedListNode getNext()  
   { 
        return next; 
    }//end node 
    public void setNext(LinkedListNode next)  
   { 
        this.next = next; 
    }//end setNext 

}//end class node

LinkedList 类:(对列表有其他方法)

public class LinkedList { 

    public LinkedListNode front; 

    public LinkedList() { 
        this.front = null; 
    } 

    public void insertBack(String data) 
   { 
        if(front == null){ 
            front = new LinkedListNode(data); 
        }else{ 
            LinkedListNode newNode = new LinkedListNode(data); 
            LinkedListNode current = front; 
            while(current.getNext() != null){ 
                current = current.getNext(); 
            } 
            current.setNext(newNode); 
        }        
    }//end insertBack 

   public void addAfter(LinkedListNode spot, String data) 
   { 
       LinkedListNode newNode; 

       newNode = new LinkedListNode(data); 

       newNode.next = spot.next; 
       spot.next = newNode; 
   }//end addAfter 

   public void addBefore(LinkedListNode spot, String data) 
   { 

   }//end addBefore    

   public void deleteAfter(LinkedListNode spot) 
   { 
       LinkedListNode nextNode; 

       nextNode = spot.next; 
       spot.next = nextNode.next; 
   }//end deleteAfter 


    public String showList() 
   { 
        int i = 0; 
        String retStr = "The nodes in the list are:\n"; 
        LinkedListNode current = front; 
        while(current != null){ 
            i++; 
            retStr += "Node " + i + " is: " + current.getData() + " and the sum is: " + current.getSum() + "\n"; 
            current = current.getNext(); 

        } 

        return retStr; 
    } 

   public LinkedListNode findTail() 
   { 
       LinkedListNode current = front; 
        while(current.getNext() != null) 
      { 
            current = current.getNext(); 
        } 
      return current; 
   }//end findTail 
}

文件在类中:

import java.util.Scanner; 
import java.io.*; 

public class fileIn 
{ 
   LinkedListNode front; 
   LinkedList myList = new LinkedList(); 
   String fname; 

   public static void main(String[] args) 
    {    
       fileIn f = new fileIn(); 
   }//end main 


   public fileIn() 
   { 
      getFileName(); 
      readFileContents(); 
      System.out.print(myList.showList()); 
   }//end namesLinkedList 

   public void readFileContents() 
    { 
        boolean looping; 
        DataInputStream in; 
        String line; 
        int j, len; 
        char ch; 

        /* Read input from file and process. */
        try 
        { 
            in = new DataInputStream(new FileInputStream(fname)); 

            looping = true; 
            while(looping) 
             { 
                /* Get a line of input from the file. */
                if (null == (line = in.readLine()))  
                { 
                    looping = false; 
                    /* Close and free up system resource. */
                    in.close(); 
                }//end if 
                else 
                { 
                myList.insertBack(line); 
                    j = 0; 
                    len = line.length();   
                }//end else 
            } /* End while. */

        } /* End try. */

        catch(IOException e)  
        { 
            System.out.println("Error " + e); 
        } /* End catch. */
    }//end readFileContents 

     public void getFileName() 
     { 
        Scanner in = new Scanner(System.in); 

        System.out.println("Enter file name please."); 
        fname = in.nextLine(); 

     }//end getFileName 

}//end class namesLinkedList
4

2 回答 2

2
for (i = 0; i < 3; i++) { 
    int j = 2; 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, j); 
    j--; 
}

You're getting the same result because the exponent is always 2. This leads to the same value for fra (15×262 + 27×262 + 10×262 = 35,152) and lal (21×262 + 10×262 + 21×262 = 35,152). Why is this?

The variable j is declared inside the loop instead of outside. The decrement at the end has no effect since it starts over at 2 at the beginning of each iteration.

You should move the declaration out of the loop:

int j = 2; 

for (i = 0; i < 3; i++) { 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, j); 
    j--; 
}

Or you could replace j with 2 - i and get rid of the extra variable entirely.

for (i = 0; i < 3; i++) { 
    a = line.charAt(i);      
    sum += Character.getNumericValue(a) * Math.pow(26, 2 - i); 
}
于 2013-11-05T19:39:35.853 回答
0

看起来你的数学是错误的。Character.getNumericValue(a) 不会像您想象的那样返回 0 到 25 之间的值。如果您想根据第 3 个字母进行排序并使用它,只需创建一个自定义 Comparator 类。

编辑:我对 getNumericValue 的工作方式是错误的,但数学仍然是错误的(见下面的评论)。

于 2013-11-05T19:44:49.460 回答