1.定义长度位5的整型数组,输入他们的值,用冒泡排序后输出.
package zuoye;
import java.util.Scanner;
public class arc{
public static void main(String[] args) {
int[] arr = { 7, 6, 3, 5, 9 };
System.out.print("冒泡排序前 :");
printArray(arr);
bubbleSort(arr);
System.out.print("冒泡排序后 :");
printArray(arr);
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.print("
");
}
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
System.out.print("第" + (i + 1) + "轮排序后:");
printArray(arr);
}
}
}
![](https://img2020.cnblogs.com/blog/1962473/202004/1962473-20200414213920782-1085735654.png)
2.定义数组{34,22,35,67,45,66,12,33},输入一个数a,查找在数组中是否存在,如果存在,输出下标,不存在输出"not found"
package zuoye;
import java.util.Scanner;
public class arc{
public static void main(String[] args) {
int[] arr = { 34, 22, 35, 67, 45, 66, 12, 33 };
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字a");
int x=sc.nextInt();
int j=0;
for(int i=0;i<arr.length;i++){
if(x==arr[i]){
System.out.println("下标为:"+i);
j=1;
}
}
if(j==0){
System.out.println("not found");
}
}
}
![](https://img2020.cnblogs.com/blog/1962473/202004/1962473-20200415085916278-548225846.png)
3.以矩阵的形式输出一个double型二维数组(长度分别为5、4,值自己设定)的值。
package zuoye;
import java.util.Scanner;
public class arc{
public static void main(String[] args) {
double [][] buffer={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};
for(int i=0;i<buffer.length;i++){
for(int j=0;j<buffer[0].length;j++){
System.out.print(buffer[i][j]+" ");
}
System.out.println();
}
}
}
![](https://img2020.cnblogs.com/blog/1962473/202004/1962473-20200415091900276-1860424347.png)
4.定义一个二维数组(长度分别为3,4,值自己设定),求该二维数组的最大值.
package zuoye;
import java.util.Scanner;
public class arc{
public static void main(String[] args) {
int arr[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int max=arr[0][0];
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
if(arr[i][j]>max){
max=arr[i][j];
}
}
}
System.out.println("二维数组中最大的是:"+max);
}
}
![](https://img2020.cnblogs.com/blog/1962473/202004/1962473-20200415094331696-174840590.png)