import java.util.Scanner;
import java.util.ArrayList;
public class JAVA {
static ArrayList <Integer> alist = new ArrayList<Integer>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner n = new Scanner(System.in);
Scanner s = new Scanner(System.in);
int i=0;
while(true)
{
int num = n.nextInt();
if(num==0)
continue;
if(num>1000 || num<0)
continue;
i++;
String a = s.nextLine();
if(a.isEmpty())
continue;
i++;
String b[] = a.split(" ");
if(num==1)
{
System.out.println(b[0]);
continue;
}
if(b.length>num)
continue;
for(int j=0;j<b.length;j++)
alist.add(j,Integer.parseInt(b[j]));
sort();
alist.clear(); //要記得清空alist
if(i==2)
{
i=0;
System.gc();
continue;
}
}
}
static void sort()
{
int i=0;
int min=0;
int times=0; //放了最小值幾次
/*for(int x=0;x<arr.length;x++)
alist.add(arr[x]);*/
int sortarray[] = new int[alist.size()];
while(true)
{
int flag; //遇到旗子就移掉index為flag元素,剩下的元素繼續比大小
if(i==0)
min = alist.get(i);
else if(i>0)
{
if(alist.get(i)<min)
min = alist.get(i);
if(i==alist.size()-1)
{
sortarray[times]=min;
times++;
flag = alist.indexOf(min);
alist.remove(flag);
i=0;
min=alist.get(i);
if(alist.size()==1) //若alist的元素個數剩下一個,把最後一個元素加到sortarray裡面就可跳出
{
sortarray[times] = min;
times=0;
break;
}
}
}
i++;
}
for(int k=0;k<sortarray.length;k++)
System.out.print(sortarray[k]+" ");
}
}
快瘋了 找不到問題在哪
然後排序我不想用內建的函式 想要自己寫出來
一直跟我說有問題
在編譯器上就沒問題啊
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at JAVA.sort(JAVA.java:65)
at JAVA.main(JAVA.java:36)