Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确定字符串中的最大值数字?_Java_String_Return Value - Fatal编程技术网

Java 如何确定字符串中的最大值数字?

Java 如何确定字符串中的最大值数字?,java,string,return-value,Java,String,Return Value,考虑以下字符串: String test= "0, 1, 3, 2, 2, 1, 1, 4, 2, 5, 1, 1, 0, 1, 241"; 最大值是最后一个值,241。由于241是字符串中的第15个数字,也是行中的最大数字,如何获取字符串中该数字的计数15 第二个例子: String test= "0, 1, 3, 2, 2, 1, 1, 4, 30, 5, 1, 1, 0, 1, 5"; 结果应该是9,因为30是最大的数字,在字符串中位于第9位。使用String拆分字符串。拆分(“,”

考虑以下字符串:

String test= "0, 1, 3, 2, 2, 1, 1, 4, 2, 5, 1, 1, 0, 1, 241";
最大值是最后一个值,
241
。由于241是字符串中的第15个数字,也是行中的最大数字,如何获取字符串中该数字的计数
15

第二个例子:

String test=  "0, 1, 3, 2, 2, 1, 1, 4, 30, 5, 1, 1, 0, 1, 5";

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

使用
String拆分字符串。拆分(“,”它将返回一个数组。查找该数组中的最大值

您应该使用
拆分此字符串,然后在数组中找到最大的一个。您将获得可用于在给定字符串中查找的最大数字字符串。

尝试将字符串拆分为这样的数组,然后从该数组中查找最大数字位置

    String[] YourArray=this.split(",");

    int largest = YourArray[0];
int largestpos;

      for(i = 0; i < YourArray.length; i++){

       if(Integer.parseint(YourArray[i]) > largest){
       largest = YourArray[i];
       largestpos=i;// This is what you need
    }
}
String[]YourArray=this.split(“,”);
int最大=数组[0];
int largestpos;
对于(i=0;i最大值{
最大=数组[i];
largestpos=i;//这就是您需要的
}
}
1)拆分字符串并将其放入数组(int类型) 2) 然后,您可以使用JavaAPI提供的任何类型的排序。
3) 您将找到所需的输出。

在下面的示例中,
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;
     }
}
String test=“0,1,3,2,2,1,1,4,2,5,1,1,0,1241”;
字符串[]testArray=test.split(“,”);
int max=Integer.MIN_值,maxIndex=0;
对于(int i=0;imax){
max=Integer.parseInt(testArray[i]);
maxIndex=i;
}
}
编辑:初始化了其他变量,并通过注释更正了一些代码

String initialString = "0, 1, 3, 2, 2, 1, 1, 4, 2, 5, 1, 1, 0, 1, 241";
        String[] sArray = initialString.split(", ");
        List<Integer> iList = new ArrayList<Integer>();
        for (String s: sArray) {
            iList.add(Integer.parseInt(s));
        }
        Collections.sort(iList);
        // finding last item's value
        System.out.println(iList.get(iList.size() - 1));
        // now finding index
        System.out.println(iList.indexOf(Integer.parseInt("241")));
第二个输出为您提供解析的最大整数的索引。 注意:为简单起见,此处不检查
NumberFormatException

更好的解决方案:


仍然使用
String.split
列表
,但实现自己的
比较器

好的,我会调查的,非常感谢这是你的密码?你试过什么解决办法吗?结果如何?@user1393500我们不是算法编写者。请说明你对逻辑的看法。什么是计数器?它应该低于
testArray.length
而不是
testArray.length-1
。你的if条件无法编译。您不能测试大于整数的字符串。请在发布答案之前阅读答案。已编辑。感谢您指出快速代码回复中的错误。x3
int max = Integer.MIN_VALUE, pos=0, i=0;
for (String s : test.split(",")) {
    if (Integer.parseInt(s) > max) { max = Integer.parseInt(s); pos = i; }
    ++i;
}
System.out.println("Position of max value is: " + (pos+1));
int max = Integer.MIN_VALUE, pos=0, i=0;
for (String s : test.split(",")) {
    if (Integer.parseInt(s) > max) { max = Integer.parseInt(s); pos = i; }
    ++i;
}
System.out.println("Position of max value is: " + (pos+1));