why the below mentioned code is note working for test case mentioned at the end of code ?

import java.util.;
class HelloWorld {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc1 = new Scanner(System.in);
int n = sc1.nextInt();
Scanner sc2 = new Scanner(System.in);
while (n != 0) {
String s = sc2.nextLine();
String[] str=s.split(" ");
ArrayList set=new ArrayList();
for(String s3:str){
set.add(Integer.parseInt(s3.trim()));
}
Collections.sort(set);
if(set.size()>0)
set.remove(set.get(set.lastIndexOf(set.size()-1)));
//set.remove((set.size())-1)why this line is not working for second test case in place of above mentioned line;
System.out.println(set.get((set.size())-1));
set.clear();
n–;
}
}
}
/
**test case
3
1 2 1
3 1 2 8
1 5 1 4 3 2 */

use ideone.com for pasting code this way it looks hell untidy

//