java 数组列表
自从我上次使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我想说你的代码将无法编译或正常工作。我会这样写:
注意风格 - 这很重要,即使是像这样的微不足道的例子。
一开始没有注意到
List
的使用。I'd say that your code won't compile or work properly. I'd write it like this:
Pay attention to style - it matters, even with trivial examples like this one.
Didn't notice the use of
List
at first.要比较
String
内容,您应该使用equals
:否则,您将比较对象引用。
To compare
String
contents you should useequals
:Otherwise, you are comparing object references.
你可以使用这个:
You can use this:
这可能不是最有效的方法,但它有效:
This is probably not the most efficient way, but it works:
一般来说,您不能(或不应该)在迭代列表时修改列表。如果是我,我会将编码值存储在一个新的(可能是临时的)ArrayList 中,然后在完成后重新分配旧的值。
如前所述,您应该使用 .equals 而不是 == 来比较字符串。另外,从列表中读取值的语法是
myArr.get(i)
;不是您使用的myArr(i)
。此外,当您使用 Scanner#next() 时,它将返回一个字符串,因此如果用户输入“1010”,整个序列将存储在单个元素中,每个字符将不会获得自己的索引。
最后,按照惯例,类名应以大写字母开头。
这是我的做法(使用控制台,而不是扫描仪)。我相信这就是您追求的效果:
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)
; notmyArr(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:
正确的:
Correct: