[英]How do I convert certain values of a 2 string array dimensional array to int and compare them with another integer in java?
I have a two dimensional array of strings and I want to convert the second column to integers and then compare each one with a number but I don't know how to do it inside a loop? 我有一个二维的字符串数组,我想将第二列转换为整数,然后将每个列与一个数字进行比较,但是我不知道如何在循环中执行此操作?
int a=Integer.parseInt(array[0][1]);
int b=Integer.parseInt(array[1][1]);
int c=Integer.parseInt(array[2][1]);
int d=Integer.parseInt(array[3][1]);
int e=Integer.parseInt(array[4][1]);
int f=Integer.parseInt(array[5][1]);
int g=Integer.parseInt(array[6][1]);
int h=Integer.parseInt(array[7][1]);
int i=Integer.parseInt(array[8][1]);
int j=Integer.parseInt(array[9][1]);
if(number>a) return true;
if(number>b) return true;
if(number>c) return true;
if(number>d) return true;
if(number>e) return true;
if(number>f) return true;
if(number>g) return true;
if(number>h) return true;
if(number>i) return true;
if(number>j) return true;
If I understand you correctly, you can do this: 如果我对您的理解正确,则可以执行以下操作:
for(int i = 0; i < array.length; i++) {
if(number == Integer.parseInt(array[i][1])) {
...do something
}
}
this compares every row's first element to number 这会将每一行的第一个元素与数字进行比较
Now its been a while since I've done java and i may mix in some c# accidentally, so if my syntax is incorrect i apologize 现在已经有一段时间了,因为我已经完成了Java,并且我可能会不小心混入一些c#,所以如果我的语法不正确,我会道歉
for(int i = 0, i = array.length, i++){
if(Integer.parseInt(array[i][1]) == number)
{
//...Do code here, i.e. assign to a new array of booleans with value i beign set to true
}
}
This is called a for loop. 这称为for循环。
basically, you have a value (i = 0) , you check the value does not satisfy some condition (that i is not the length of the array) and if it doesnt satisfy that condition, perform an operation, then iterate to the next value, in this case making i equal to i + 1. 基本上,您有一个值(i = 0),请检查该值不满足某些条件(即我不是数组的长度),如果不满足该条件,请执行操作,然后迭代到下一个值,在这种情况下,使i等于i + 1。
This allows you to iteratively check through a list or array. 这使您可以迭代检查列表或数组。
If i get any terms/syntax wrong feel free to correct me in comments or edit my post, Thanks. 如果我有任何错误的术语/语法,请随时在评论中纠正我或编辑我的帖子,谢谢。 hopefully i answered your question
希望我回答了你的问题
String[][] array = new String[ROWSIZE][COLUMNSIZE]
for(int i = 0; i < ROWSIZE; i++){
if(number > Integer.parseInt(array[i][1])){
//do something
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.