java 数组列表

发布于 2024-12-09 14:34:30 字数 798 浏览 0 评论 0原文

自从我上次使用 java 以来已经有一段时间了,我正在尝试温习一些东西。

import java.util.*;

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

        Scanner inputBitString = new Scanner(System.in);        

        //input.
        String binArray;

        ArrayList<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"
            }
        }

    }

}

所以我想将用户键盘输入的输入存储在数组中。因此,如果用户输入“1010”,for 循环将遍历数组,并将“1”替换为“10”,将“0”替换为“01”。 所以结果输出将是“10011001”

谢谢

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<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

话少情深 2024-12-16 14:34:30

我想说你的代码将无法编译或正常工作。我会这样写:

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

注意风格 - 这很重要,即使是像这样的微不足道的例子。

一开始没有注意到List的使用。

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.

凉城 2024-12-16 14:34:30

要比较 String 内容,您应该使用 equals

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

否则,您将比较对象引用。

To compare String contents you should use equals:

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

Otherwise, you are comparing object references.

音栖息无 2024-12-16 14:34:30

你可以使用这个:

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");
}

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");
}
£噩梦荏苒 2024-12-16 14:34:30

这可能不是最有效的方法,但它有效:

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 );
    }
}

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 );
    }
}
月下凄凉 2024-12-16 14:34:30

一般来说,您不能(或不应该)在迭代列表时修改列表。如果是我,我会将编码值存储在一个新的(可能是临时的)ArrayList 中,然后在完成后重新分配旧的值。

如前所述,您应该使用 .equals 而不是 == 来比较字符串。另外,从列表中读取值的语法是myArr.get(i);不是您使用的 myArr(i)

此外,当您使用 Scanner#next() 时,它将返回一个字符串,因此如果用户输入“1010”,整个序列将存储在单个元素中,每个字符将不会获得自己的索引。

最后,按照惯例,类名应以大写字母开头。

这是我的做法(使用控制台,而不是扫描仪)。我相信这就是您追求的效果:

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);

    }



}

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);

    }



}
隔岸观火 2024-12-16 14:34:30

正确的:

    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");
    }

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");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文