保存路径:C:\Users\10198\Desktop\java\TM\sl\5\12
知识①:在学习的过程中需要用到遍历数组
public class Test
{
public static void main(String[] args)
{
int[] array = { 63, 4, 24, 1, 3, 15 };
for (int i : array ) //遍历数组
{
System.out.printf("%d\t",i);
}
System.out.println();
}
}
程序整体:
public class Maopao
{
public static void main(String[] args)
{
int []array = { 63 , 24 , 70 , 90 , 45 }; //定义一个需要被排序的数组
Maopao sorter = new Maopao(); //创建对象
sorter.sort(array); //对数组排序
//Maopao.sort(array); 是不对的,因为Maopao是类名,通过类名无法调用下文非静态的成员方法,只有对象(实例)才能调用其非静态成员方法
sorter.Show(array); //输出数组
//sorter.Show(array); 同理,Maopao.Show(array); 也是不对的,如果用,则要将成员方法设置为static静态
}
public void sort(int []array)
{
for (int i= 1 ;i<array.length ;i++ )
{
for (int j = 0 ;j< array.length - i ;j++ )
{
if (array[j] > array[j + 1])
{
int temp = array[j]; // 把第一个元素值保存到临时变量中
array[j] = array[j + 1]; // 把第二个元素值保存到第一个元素单元中
array[j + 1] = temp; // 把临时变量(也就是第一个元素原值)保存到第二个元素中
}
}
}
}
public void Show(int []array)
{
for (int i : array ) //遍历数组array,其中array是该方法接受的形参数组
{
System.out.print(" >" + i); // 输出每个数组元素值
}
}
}