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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16650205/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:34:02  来源:igfitidea点击:

How do I determine the largest value number in a String?

javastringreturn-value

提问by user1393500

Consider the following String:

考虑以下字符串:

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

The largest value is the last value, 241. How do I get the count, 15, of this number within the String, as 241 is the 15th number in the String, and the largest number in the row?

最大值是最后一个值,241。我如何获得15字符串中这个数字的计数,因为 241 是字符串中的第 15 个数字,也是该行中的最大数字?

Second example:

第二个例子:

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

The result should be 9 as 30 is the largest number and in the 9th place in the String.

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

回答by Krauxe

In the following example, the maxIndexvariable will contain the index of the highest value in the array and the actual position will be maxIndex + 1, which is what you are looking for.

在以下示例中,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;
     }
}

Edit: initialized the other variables, and corrected some code thanks to comments

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

回答by Blackbelt

split the string with String.split(",");it will return an array. Look for the max inside that array

String.split(",");它拆分字符串将返回一个数组。在该数组中查找最大值

回答by Michal Borek

You should split this string with ,, then find the biggest one in an array. You will get the biggest number string that can be applied to find in given string.

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

回答by Basim Sherif

try splitting your string into an array like this,Then find the largest number postion from that array

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

    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
    }
}

回答by SilentBomb

1) You split your string and put it into an array (of int type) 2) Then you can use any kind of Sorting provided by JAVA API(s). 3) You will find your desired output.

1) 您拆分字符串并将其放入一个数组 (int 类型) 2) 然后您可以使用 JAVA API(s) 提供的任何类型的排序。3) 你会找到你想要的输出。

回答by Mena

Quick and ugly:

又快又丑:

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")));

Output:

输出:

241
14

The second output gives you the index of the largest Integer parsed. Note: not checking for NumberFormatExceptionhere for simplicity.

第二个输出为您提供解析的最大整数的索引。注意:NumberFormatException为了简单起见,这里不检查。

Better solution:

更好的解决方案:

Still use String.splitand a List, but implement your own Comparator.

仍然使用String.splitand a List,但实现您自己的Comparator.

回答by xagyg

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));