开发者

java array list [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

its been awhile since i used java the last time and am trying to brush up on couple of things.

import java.util.*;

public class bitStrings {
    public static void main(String [] args){

        Scanner inputBitString = new Scanner(System.in);        

        //input.
        String binArray;

        ArrayList<开发者_JS百科;String> myArr = new ArrayList<String>();

        while(inputBitString.hasNext()){

            binArray=inputBitString.next();         
            myArr.add(binArray);
            System.out.println(myArr);      

            for(int i=0;i<myArr.size();i++){
                if(myArr(i)=="1") myArr(i)=="10";
                else myArr(i)=="01"
            }
        }

    }

}

So I want to store the input in an array from the user's keyboard input. so if the user types "1010", the for loop will go through the array and replaces the "1" with "10" and "0" with "01". so the resulting output will be "10011001"

Thanks


I'd say that your code won't compile or work properly. I'd write it like this:

if("1".equals(myArr.get(i))) {
    myArr.set(i, "10");
} else {
    myArr.set(i, "01");
}

Pay attention to style - it matters, even with trivial examples like this one.

Didn't notice the use of List at first.


To compare String contents you should use equals:

myArr.get(i).equals( "1" );

Otherwise, you are comparing object references.


You can use this:

ArrayList<String> myArr = new ArrayList<String>();
ArrayList<String> myArrDub = new ArrayList<String>();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();

for (int i=0; i<str.length(); i++) {
    String tempStr = new String();
    tempStr += str.charAt(i);
    myArr.add(tempStr);
}

System.out.println(myArr);

for(int i=0;i<myArr.size();i++){
    if(myArr.get(i).equals("1"))
    myArrDub.add("10");
else
    myArrDub.add("01");
}


This is probably not the most efficient way, but it works:

public class Main {

    public static void main( String[] args ) {
        String user = "1010";
        StringBuilder sb = new StringBuilder();
        for ( char c : user.toCharArray() ) {
            if ( c == '1' )
                sb.append( "10" );
            else
                sb.append( "01" );
        }
        System.out.println( sb );
    }
}


In general you cannot (or shouldn't) modify a list while you're iterating over it. If it were me, I would store the encoded values in a new (possibly temporary) ArrayList and then reasign the old one once finished.

As has been stated you should use .equals not == to compare strings. Also, the syntax for reading a value from a List is myArr.get(i); not myArr(i) as you have used.

Additionally, when you use Scanner#next() it will return a String, so if a user entered "1010" the entire sequence would be stored in a single element, each character would not get its own index.

Finally, Class names, by convention, should begin with a capital letter.

Here is how I would do it (using Console, not scanner). I believe this is the effect you're after:

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


public class BitStrings{

    public static void main(String[] args){

        Console c = System.console();
        List<String> myArr = new ArrayList<String>();
        if(null == c){ 
            //exit gracefully
        }

        String entry = c.readLine("Enter the bits: "); // Wait for user entry

        for(int i = 0; i < entry.length(); i++){
            String bit = entry.substring(i, i+1);
            if("1".equals(bit)) myArr.add("10");
            else myArr.add("01");
        }
        //If we want to print it out
        for(String element : myArr) System.out.print(s);

    }



}


Correct:

    ArrayList<String> myArr = new ArrayList<String>();
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str = bf.readLine();
    for (int i=0; i<str.length(); i++)
        myArr.add("" + str.charAt(i));

    for(int i=0;i<myArr.size();i++){
        if(myArr.get(i).equals("1"))
            myArr.set(i, "10");
        else
            myArr.set(i, "01");
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜