如何确定字符串中的最大值编号?

use*_*500 -7 java string return-value

请考虑以下字符串:

String test= "0, 1, 3, 2, 2, 1, 1, 4, 2, 5, 1, 1, 0, 1, 241";
Run Code Online (Sandbox Code Playgroud)

最大值是最后一个值241.如何15在字符串中获取此数字的计数,因为241是字符串中的第15个数字,并且是行中的最大数字?

第二个例子:

String test=  "0, 1, 3, 2, 2, 1, 1, 4, 30, 5, 1, 1, 0, 1, 5";
Run Code Online (Sandbox Code Playgroud)

结果应该是9,因为30是最大的数字,而在字符串中的第9位.

Kra*_*uxe 6

在下面的示例中,maxIndex变量将包含数组中最高值的索引,实际位置将maxIndex + 1是您要查找的内容.

String test = "0, 1, 3, 2, 2, 1, 1, 4, 2, 5, 1, 1, 0, 1, 241";
String[] testArray = test.split(", ");

int max = Integer.MIN_VALUE, maxIndex = 0;

for (int i = 0; i < testArray.length; i++) {
     if (Integer.parseInt(testArray[i]) > max) {
         max = Integer.parseInt(testArray[i]);
         maxIndex = i;
     }
}
Run Code Online (Sandbox Code Playgroud)

编辑:初始化其他变量,并通过评论更正了一些代码


Bla*_*elt 5

String.split(",");它拆分字符串将返回一个数组.寻找该数组内的最大值