有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中字符串列表的最大值

我有以下形式的数据:

String[] values = {"4,8", "1,6", "7,8", "1,5"}

其中,我必须找到第二个元素的最大值,如果有两个以上的最大值(“4,8”和“7,8”),找到第一个元素的最小值。因此,值的输出应该是字符串“4,8”

我是JAVA新手,我不知道该如何做。我试着用stringsplit之类的东西

        int[] num = new int[values.length];
        int[] num2 = new int[values.length];

        for (int i = 0; i<values.length; i++){
            String[] test = values[i].split(",");
            int nummed = Integer.parseInt(test[0]);
            int nummed2 = Integer.parseInt(test[1]);
            num[i] = nummed;
            num2[i] = nummed2;

            //System.out.println(test[0]);
            //System.out.println(test[1]);
        }

但它很快变得非常复杂,我需要知道索引,或者过滤掉数据以找到第一项的最小值


共 (4) 个答案

  1. # 1 楼答案

    public class StringManipulation {
      public static void main(String args[]) {
        System.out.println(output());
      }
    
      private static String output() {
        String[] values = {"4,8", "1,6", "7,8", "1,5"};
        int max = Integer.MIN_VALUE;
        int first = Integer.MAX_VALUE;
        for(int i = 0; i < values.length; i++) {
          String[] arr = values[i].split(",");
          if(Integer.parseInt(arr[1]) >= max){
            max = Integer.parseInt(arr[1]);
            first = Integer.parseInt(arr[0]) < first ? Integer.parseInt(arr[0]):first;
          }
        }
        return (first) + "," + (max);
      }
    }
    

    解决这个问题可能有多种方法。鉴于我对这个问题的理解,这是最简单的解决方案之一

  2. # 2 楼答案

    继续你的解决方案,经过测试。正如预期的那样,很难在手机上全部输入

    public static void main(String arg[])
    {
    
    String[] values = {"4,8", "1,6", "7,8", "1,5","2,8"};
      int[] num = new int[values.length];
            int[] num2 = new int[values.length];
    
            for (int i = 0; i<values.length; i++){
                String[] test = values[i].split(",");
                int nummed = Integer.parseInt(test[0]);
                int nummed2 = Integer.parseInt(test[1]);
                num[i] = nummed;
                num2[i] = nummed2;
    
                //System.out.println(test[0]);
                //System.out.println(test[1]);
            }
    
        int max=0;
        int min=0;
        for(int i=0;i<num2.length;i++)
        {
         if(num2[i]>max) {
           max =num2[i];
           min=num[i];
         }
          else if(num2[i]==max)
            min = min>=num[i]?num[i]:min;
        }
       System.out.println(min+","+max);
     }
    }
    
  3. # 3 楼答案

    这应该足够了

    class Main {
        public static void main(String args[]) {
            String[] values = {"4,8", "1,6", "7,8", "1,5"}; // try {"4,8", "1,6", "7,8", "1,5", "1,9"}
            int right = Integer.MIN_VALUE;
            int left = Integer.MAX_VALUE;
            for (int i = 0; i<values.length; i++){
                String[] test = values[i].split(",");
                int nummed = Integer.parseInt(test[0]);
                int nummed2 = Integer.parseInt(test[1]);
                if (nummed2 >= right) {
                    if (right != nummed2) {
                        left = Integer.MAX_VALUE;
                    }
                    right = nummed2;
                    if (nummed < left) {
                        left = nummed;
                    }
                }
            }
            System.out.println(left + "," + right);
        }
    }
    
  4. # 4 楼答案

    下面是一个利用fluentComparatorapi、stream api和BigDecimals的解决方案:

            String[] values = {"4,8", "1,6", "7,8", "1,5"};
    
            // create a custom comparator
            Comparator<BigDecimal> comparator = Comparator
                    // first, comparing only the right side - the remainders
                    .comparing((BigDecimal num) -> num.remainder(BigDecimal.ONE))
                    // then, comparing the left side - the decimal part
                    .thenComparing(num -> num.setScale(0, RoundingMode.DOWN));
    
            // find the max value using java stream api
            Arrays.stream(values)
                    // replace commas with dots
                    .map(num -> num.replace(',', '.'))
                    // map values to bigdecimal
                    .map(BigDecimal::new)
                    // find the max element by our custom comparator
                    .max(comparator)
                    // print if an element is found (if the array was not empty)
                    .ifPresent(System.out::println);