import java.util.*;
public class Bubble {
private ArrayList<Integer>array;
public Bubble() {
// TODO Auto-generated constructor stub
this.array =new ArrayList<Integer>();
}
public ArrayList<Integer> bubble(ArrayList<Integer>arr){
ArrayList<Integer> array = arr;
if(array==null){
System.out.println("排序数组不存在");
}else if(array.size()==1){
System.out.println(array.get(0));
}
for(inti=0;i<array.size();i++){
for(intj=0;j<array.size()-1;j++){
if(array.get(j)>array.get(j+1)){
intt = array.get(j);
array.set(j,array.get(j+1));
array.set(j+1,t);
}
}
}
return array;
}
///输入数组元素,输入N结束输入
public void inputItem(){
System.out.println("请依次输入数组数值,每次输入一个,回车录入数值,输入N结束输入进行排序");
Scanner sc =new Scanner(System.in);
booleancanContinueInput = true;
while(canContinueInput){
String strInput =sc.nextLine();
if(strInput.equals("N")){
canContinueInput =false;
}else{
try{
int num = Integer.parseInt(strInput);
this.array.add(num);
// System.out.println("输入为:"+num);
}catch(Exceptione){
System.out.println("输入有误");
}
}
}
System.out.println("待排序数组:"+this.array);
}
public ArrayList<Integer> getArray() {
returnarray;
}
public void setArray(ArrayList<Integer> array) {
this.array =array;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Bubble bubble =new Bubble();
bubble.inputItem();
ArrayList<Integer> result = bubble.bubble(bubble.getArray());
bubble.setArray(result);
System.out.println("排序结果:"+result);
// System.out.println("排序结果:"+bubble.getArray());
}
}