Java 字符串数组以及如何循环它们
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9688264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String Arrays and how to loop them
提问by Eric Goncalves
I was wondering if it is possible to make a loop for an array of strings that i want to evaluate in my code. I want to do more than one binary number at a time. So far I have this working correctly, however I cant figure our how to make it evaluate more than one binary number at a time. Thank you.
我想知道是否可以为我想在我的代码中评估的字符串数组创建一个循环。我想一次做多个二进制数。到目前为止,我的这个工作正常,但是我无法弄清楚如何让它一次评估一个以上的二进制数。谢谢你。
package twoComplement;
public class bintodec {
public static void main (String[] args)throws java.io.IOException {
int number,
digit1,
digit2,
digit3,
digit4,
digit5,
digit6,
digit7,
digit8,
result;
String num = "11100111";
number = Integer.parseInt(num);
digit1 = ((number % 100000000) - (number % 10000000 % 10000000)) / 10000000;
digit2 = ((number % 10000000) - (number % 10000000 % 1000000)) / 1000000;
digit3 = ((number % 1000000) - (number % 1000000 % 100000)) / 100000;
digit4 = ((number % 100000) - (number % 100000 % 10000)) / 10000;
digit5 = ((number % 10000) - (number % 10000 % 1000)) / 1000;
digit6 = ((number % 1000) - (number % 1000 % 100)) / 100;
digit7 = ((number % 100) - (number % 100 % 10)) / 10;
digit8 = (number % 10);
result = (digit1 * -128) + (digit2 * 64) + (digit3 * 32) + (digit4 * 16) + (digit5 * 8) + (digit6 * 4) + (digit7 * 2) + (digit8 * 1);
System.out.println ( "Binary number: " + num + "\nDecimal Number: " + result);
System.out.println();
System.exit( 0 );
}
}
采纳答案by bbedward
Yes, this is the purpose of loops;-) An advanced forloop is the best way to iterate through an array. You can also iterate through collections (IE: ArrayList
), which makes it easier to add new items.
是的,这就是循环的目的;-)高级 for循环是遍历数组的最佳方式。您还可以遍历集合 (IE:) ArrayList
,这样可以更轻松地添加新项目。
String[] numbersToEvaluate = new String[]{"11100111", "100101", "10101101"};
for (String num: numbersToEvaluate)
{
number = Integer.parseInt(num);
digit1 = ((number % 100000000) - (number % 10000000 % 10000000)) / 10000000;
digit2 = ((number % 10000000) - (number % 10000000 % 1000000)) / 1000000;
digit3 = ((number % 1000000) - (number % 1000000 % 100000)) / 100000;
digit4 = ((number % 100000) - (number % 100000 % 10000)) / 10000;
digit5 = ((number % 10000) - (number % 10000 % 1000)) / 1000;
digit6 = ((number % 1000) - (number % 1000 % 100)) / 100;
digit7 = ((number % 100) - (number % 100 % 10)) / 10;
digit8 = (number % 10);
result = (digit1 * -128) + (digit2 * 64) + (digit3 * 32) + (digit4 * 16) + (digit5 * 8) + (digit6 * 4) + (digit7 * 2) + (digit8 * 1);
System.out.println ( "Binary number: " + num + "\nDecimal Number: " + result);
}
回答by AlexR
This is the way to define string array:
这是定义字符串数组的方法:
int n = 10;
String[] arr = new String[n];
And here is how to iterate over array:
这是遍历数组的方法:
for (int i = 0; i < n; i++) {
arr[i] = "element number " + i;
}
or this way:
或者这样:
for (String element : arr) {
System.out.println(element);
}
回答by Charlie
If you are running at least Java 5, you can use a for-eachconstruct to loop over any arbitrary Array or Collection.
如果您至少运行 Java 5,则可以使用for-each构造循环遍历任意数组或集合。
String[] nums = //init
for (String num:nums) {
// Do work.
}
If, however, you are on Java 1.4 or earlier, or you care about the index of the array, you need to use a traditional for loop
但是,如果您使用的是 Java 1.4 或更早版本,或者您关心数组的索引,则需要使用传统的 for 循环
String[] nums = //init
for (int i = 0; i<nums.length; i++) {
String num = nums[i];
// Do work.
}
回答by Peter Lawrey
Do you mean something like
你的意思是像
for (String num : "11100111,1010,11111,110101010101010101011101010101010101010101".split(",")) {
long result = Long.parseLong(num, 2);
System.out.println("Binary number: " + num + ", Decimal Number: " + result);
}
prints
印刷
Binary number: 11100111, Decimal Number: 231
Binary number: 1010, Decimal Number: 10
Binary number: 11111, Decimal Number: 31
Binary number: 110101010101010101011101010101010101010101, Decimal Number: 3665040856405
回答by Dunes
Yep, that's pretty easy.
是的,这很容易。
String[] array = {"first", "second", "third"};
for (String element : array) {
// do whatever
}
As an aside, however, you may wish to consider the following for clearer code.
但是,顺便说一句,您可能希望考虑以下内容以获得更清晰的代码。
String num = "1000";
int digit1 = Character.digit(num.charAt(0), 2);
// if you are only expecting a binary number then the second argument (2)
// tells the digit method to throw an error if it gets a digit other than 0 or 1
...
If this is for a school exercise or similar you may wish to test your code against the builtin Integer parser.
如果这是用于学校练习或类似的,您可能希望针对内置的整数解析器测试您的代码。
int numFromBinaryString = Integer.parseInt(numberString, 2);
// again, here the second argument tells parseInt to interpret numberString as
// binary string