2014-01-30 98 views
1
horse1ProgressionStr = Integer.toString(horse1Progression); 
    horse2ProgressionStr = Integer.toString(horse2Progression); 
    horse3ProgressionStr = Integer.toString(horse3Progression); 
    horse4ProgressionStr = Integer.toString(horse4Progression); 




    List<List<String>> list = Arrays.asList(Arrays.asList(horse1ProgressionStr, horse2ProgressionStr, horse3ProgressionStr, horse4ProgressionStr)); 
    for (List<String> l : list) { 
     Collections.sort(l); 
    } 
    Collections.sort(list, new Comparator<List<String>>() { 
     public int compare(List<String> o1, List<String> o2) { 
      return o1.get(0).compareTo(o2.get(0)); 
     } 
    }); 


    System.out.println("" + list); 


    List<List<String>> fourthHorse = list.subList(0, 2); 
    List<List<String>> thirdHorse = list.subList(2, 4); 
    List<List<String>> secondHorse = list.subList(4, 6); 

我一直试图把4个整数按顺序,他们把它们放到单独的变量,但我尝试它的每一个方式似乎给我一个越界例外。subListString索引超出界限

异常=

异常在线程 “主” java.lang.IndexOutOfBoundsException:toIndex = 2

+1

你在列表中有一个**列表'''列表' –

回答

0

在问题的代码是过于复杂,只是这样做:

int[] horses = { horse1Progression, horse2Progression, 
       horse3Progression, horse4Progression }; 

// sort the input array in ascending order 
Arrays.sort(horses); 

// retrieve the elements in each position 
int first = horses[0]; 
int second = horses[1]; 
int third = horses[2]; 
int fourth = horses[3]; 
+1

非常感谢你,我一直在努力做到这一点! – user3254808

+0

@ user3254808不客气!如果此答案解决了您的问题,请不要忘记通过点击复选标记[接受](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)往左边 ;) –

0

的对象list的大小为4,但您试图检索列表到6索引list.subList(4, 6)