例如:String s[] = {"150","45","250","232","244","189","200","219","228"};
正常情况下,取得数组的最大值的算法比较多,也是很好实现的,现在要求次大值在数组中的下标,譬如上面的数据
最大值(250)的下标为2,次大值(244)的下标为4,
这个如何取得次大值的大小及下标呢?
希望哪位达人给个算法参考参考谢谢!

解决方案 »

  1.   

      public static void main(String[] args) {
        String s[] = { "150", "45", "250", "232", "244", "189", "200", "219", "228" };
        int max = Integer.MIN_VALUE;
        int next = Integer.MIN_VALUE;
        int indexMax = 0;
        int indexNext = 0;
        int index = 0;
        int temp;
        for (String ss : s) {
          temp = Integer.parseInt(ss);
          if (temp > max) {
            next = max;
            max = temp;
            indexNext = indexMax;
            indexMax = index;
          } else if (temp > next) {
            next = temp;
            indexNext = index;
          }
          index++;
        }
        System.out.println(indexMax + "=" + max);
        System.out.println(indexNext + "=" + next);
      }
      

  2.   

    int max,max2nd;if(s[0]>s[1]){
       max=0;
       max2nd=1;
     }else{
       max=1;
       max2nd=0;
     }for(int i=2;i<=s.length;i++){
       if(s[i]>s[max2nd]){
            if(s[i]>s[max]){
                 max2nd=max;
                 max=i;
            }else{
                 max2nd=i;
            }
       }
    }
      

  3.   

    package f.test;
    class Sort {

    /**
     * Method main
     *
     *
     * @param args
     *
     */
    public static void main(String[] args) {
    // TODO: Add your code here
    String s[]   =   {"150","45","250","232","244","189","200","219","228"};
    int length=s.length;
    Integer inter[] =new Integer[length];
    /*
     *首先将字符数组转换为整数型数组,编译在后面通过Arrays的sort方法进行排序
     *同时将数组中的元素的位置存储在映射中,编译排序后根据元素值获得元素的位置
     */
    java.util.Map position=new java.util.HashMap();
    for(int i=0;i<length;i++){
    position.put(s[i],new Integer(i));
    inter[i]=new Integer(s[i]);
    }
    /*
     *对数组按升序排序,关于sort的详细用法看API 文档
     */
    java.util.Arrays.sort(inter);
    System.out.println("次大值为 :"+inter[length-2]+",次大值下标为 :"+position.get(inter[length-2].toString())); 
    }
    }
      

  4.   

    来晚了,偶靠,
    java2000_net  方法不错
      

  5.   

    /*
     * Created on 2008/01/09
     */public class numTest {
        public static void main(String[] args) {        int s[]   =   {150,45,250,232,244,189,200,219,228};
            int max = 0;
            int No2max = 0;
            for (int i = 0; i < s.length; i++) {
                if(max > s[i])
                {
                }else{
                    max = s[i];
                }
                
            }
            System.out.println(max);
            for (int j = 0; j < s.length; j++) {
                if(s[j] == max && j!=s.length-1)
                {
                    j++;
                }else if(s[j] == max && j == s.length-1)
                {
                    break;
                }
                if(No2max > s[j])
                {
                }else
                {
                    No2max = s[j];
                }
            }
            System.out.println(No2max);
        }
        
    }
      

  6.   

    不好意思,我说差点什么呢,没有下标
    /*
     * Created on 2008/01/09
     */public class numTest {
        public static void main(String[] args) {        int s[]   =   {150,45,250,232,244,189,200,219,228};
            int max = 0;
            int No2max = 0;
            int maxIndex= 0;
            int max2Index =0;
           for (int i=0; i < s.length; i++) {
                if(max > s[i])
                {
                }else{
                    max = s[i];
                    maxIndex = i+1;
                }
                
            }
          
            System.out.println(max + "("+maxIndex +")");
            
            for (int j = 0; j < s.length; j++) {
                if(s[j] == max && j!=s.length-1)
                {
                    j++;
                }else if(s[j] == max && j == s.length-1)
                {
                    break;
                }
                if(No2max > s[j])
                {
                }else
                {
                    No2max = s[j];
                    max2Index = j+1;
                }
            }
            System.out.println(No2max+"("+max2Index +")");
        }
        
    }
      

  7.   

    都不错,这几个算法都差不多,在两个之间进行比较的时候取出最大值的同时也可以取出最大值相对应的下标(length).循环一次结果就出来了
      

  8.   

    String 类型的对象能否比较大小,我不知道。但我知道整数型能。
    impor java.util.*;
    public class test{
    public static void main(String args[]){
    int   s[]   =   {150,45,250,232,244,189,200,219,228}; 
    for(int i=0;i<s.length-1;i++){
      for(int j=i+1;j<s.length;j++){
           if(s[i]>s[j]){
           int temp=s[i];
           s[j]=temp;
            s[i] =s[j];
          }
       }
    }
    int k=s[s.length()-1];
    HashMap has=new HashMap();
    has.put(150,"1");
    has.put(45,"2");         
    has.put(250,"3");
    has.put(232,"4");
    has.put(244,"5");
    has.put(189,"6");
    has.put(200,"7");
    has.put(219,"8");
    has.put(228,"9");
    System.out.println("次大值为:"+has.get(k));
        }
    }