Java中的二进制到Int链接列表

时间:2015-05-10 06:50:10

标签: java linked-list

我创建了一个链接列表,用于在String中存储二进制数并将其转换为单个位并存储在每个链接中。我的问题是我已经完成了所有这些,现在我正在尝试创建一个方法来取位并将其转换为整数。

问题是二进制到整数逻辑无法正常工作。 例如:如果我输入二进制值00000001,我会很好地打印我的列表0|0|0|0|0|0|0|1,但我的整数总数会回复为' 0'什么时候应该是' 1'

我的主要方法

    private  static Scanner input =new Scanner (System.in);

public static void main(String[] args) {
    // TODO Auto-generated method stub
    LinkedList LL=new LinkedList();

    char c=' '; 
    int i=0;
    int total=0;

    System.out.println("Enter a  8-Bit Binary Number");
    String s=input.next();

    if(s.length()<8 ||s.length()>8 || s.length()<0 || s.length()==0){
        System.out.println("Error");    
    }

    while(i<s.length()){//start while : breaks string into single bits and stores into a link individually
        c=s.charAt(i);
        LL.addfromTail(new LinkData(c));
        LL.BinarytoInt(new LinkData(c));
        i++;    
    }//end while

    LL.PrintList();
    System.out.println();

我的LinkedList类

             public class LinkedList {
Link head=null;
int total=0;




    void PrintList(){//start method
        Link curr=head;

        while(curr!=null){//start while
            System.out.print(curr.ld+"|");
            curr=curr.next;
        }//end while
    }//end method



    void addfromHead(LinkData n){//start method

        Link nl=new Link(n);
        if(head==null){
            head=nl;
        }

        else{
            nl.next=head;
            head=nl;
        }
    }//end method



    void addfromTail(LinkData n){
        Link nl=new Link(n);

        if(head==null){
            head=nl;
        }
        else{
            Link curr=head;
            while(curr.next!=null){
                curr=curr.next;
            }
            curr.next=nl;
        }
    }



    /* int BinarytoInt(LinkData ld2){
        Link curr=new Link(ld2);
         curr=head;
        int x=1;
        while(curr.next!=null){
            if(curr.ld.binarybit=='1'){
                total=total+(x*1);
            }
            x=x*2;
            curr=curr.next;
        }//end while
        System.out.println(total);
        return total;
    }
*/



    /*void BinarytoInt(char c){

        Link curr=head;

         int value=128;

         while(curr.next!=null){
             if(c=='1'){
                 total=total+(value*1); 
                 curr=curr.next;
             }//end if

             value=value/2;
         }//end while

        System.out.println(total);
    }*/







}

1 个答案:

答案 0 :(得分:1)

看起来问题是您使用单个临时节点调用BinarytoInt(),而不是遍历整个LinkedList

只需在方法开头设置currhead,就不需要将临时节点作为参数:

//Remove parameter, it's not needed:
void BinarytoInt(){
    int x=128; int i=0;int total=0;
    //Link curr=new Link(temp);
    Link curr = head; //set curr to head reference

    while(curr.next!=null){
        if(curr.ld.binarybit=='1'){
            //total=total+(x*1);  //this is fine
            total += x;  //more elegant
        }
        curr=curr.next;
        x=x/2;  
    }//end while
    System.out.println(total);
}//end method

然后,不是在每次迭代时调用BinarytoInt(),而是在填充整个列表后执行此操作。为了使每个2的倍数与每个列表项正确对齐,这是必要的:

while(i<s.length()){
    c=s.charAt(i);
    LL.addfromTail(new LinkData(c));
    //LL.BinarytoInt(new LinkData(c)); //remove this
    i++;    
}//end while

LL.BinarytoInt(); //Do this after the list is populated

LL.PrintList();
System.out.println();