数组是相同数据类型的数据按顺序组成的一种引用数据类型。
声明一维数组:数组元素数据类型 [ ] 数组名;
声明二维数组:数组元素数据类型[ ][ ] 数组名;
例子:
package array;
import java.util.concurrent.SynchronousQueue;
public class Test {
public static void main(String[] args) {
//double [] scores=new double[3];
//scores[0]=99.0;
//scores[1]=98.0;
//scores[2]=97;//数据类型转换为double型
//System.out.println(scores[2]);
//声明
//double scores[];
//String accounts[][];
//穷举法
//double [] scores= {1,2,3};
//double [] scores=new double[] {1,2,2};
//String [] [] accounts= {{"xiao","12345"},{"the","558522"},{"skwj","553488"}};
//String [] [] accounts=new String [] [] {{"xiao","12345"},{"the","558522"},{"skwj","553488"}};
//指定长度
//double [] scores=new double[2];
// System.out.println(scores[0]);
//String [] [] account=new String [2][2];
//double [] scores=new double[3];
//如何赋值
//scores[0]=8.9;
//scores[1]=7.9;
//scores[2]=8.8;
//如何获取值
//System.out.println(scores[2]);//由于数组内存空间是连续的且从下标0开始,所以可以使用for循环遍历数组:
//如何遍历数组
//int i;
//for(i=0;i<3;i++) {
// System.out.println(scores[i]);
//}
//for(double score:scores) {
// System.out.println(score);
//}
//String [] [] accounts= {{"xiao","12345"},{"the","558522"},{"skwj","553488"}};
//for(String [] account:accounts) {
// for(String str:account) {
// System.out.print(str+"\t");
// }
// System.out.println();
//}
//for (int i = 0; i < 3; i++) {
// for (int j = 0; j < 2; j++) {
// System.out.println(accounts[i][j]);
// }
//}
//System.out.println(accounts.length);
//for (int i = 0; i < accounts.length; i++) {
// String [] account=accounts[i];
// System.out.println(account.length);
// for (int j = 0; j < account.length; j++) {
// System.out.print(account[j]+"\t");
// }
// System.out.println();
//}
}
}
冒泡排序法:
是最基本的排序法之一,冒泡排序法的运行机制是通过循环遍历元素并调整相邻元素顺序来实现的一种简单排序方法。
插入排序法:
每循环一次都将一个待排序的元素所对应的数据按其顺序大小插入到前面已经排序的序列的合适位置,直到全部插入排序完为止,其难点在于如何在前面已经排好序的序列中找到合适的插入位置。该排序方法有很多,比如直接插入排序、二分插入排序、希尔排序等等。
例子:
package array;
import java.util.concurrent.SynchronousQueue;
public class Test {
public static void main(String[] args) {
int [] scores=new int[] {1,2,3,4,5,6,7,8,9,10};
for (int x = 0; x < scores.length; x++) {
if((x % 2)==0) {
System.out.print(scores[x]+"\t");
}
}
System.out.println();
System.out.println();
int [] [] accounts=new int [6][6];
int sum=0;
for (int i = 0; i < accounts.length; i++) {
for (int j = 0; j < accounts.length; j++) {
System.out.print((i*6+j+1)+"\t");
if((i==j)||((i+j)==5)) {
sum=sum+(i*6+j+1);
}
}
System.out.println();
}
System.out.println("对角线的和为:"+sum);
System.out.println();
int [] score=new int [5];
int change;
score[0]=21;
score[1]=99;
score[2]=3;
score[3]=1024;
score[4]=16;
for (int i = 0; i < score.length-1; i++) {
for (int j = i+1; j < score.length; j++) {
if(score[j]<score[i]) {
change=score[i];
score[i]=score[j];
score[j]=change;
}
break;
}
}
for (int i = 0; i < score.length; i++) {
System.out.print(score[i]+"\t");
}
System.out.println();
// System.out.println();
// int [] arr=new int [5];
// int chan;
// arr[0]=1;
// arr[1]=2;
// arr[2]=4;
// arr[3]=5;
// arr[4]=0;
// for (int i = 2; i