目录
练习01:对10个整数进行按照从小到大的顺序排序
练习02:求一个3*3矩阵对角线元素之和
练习03:有一个已经按升序排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
练习04:将一个数组逆序输出。
练习05:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
练习06:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。
练习07:定义一个4行4列的二维数组,逐个从键盘输入值,然后将第1行和第4行的数据进行交换,将第2行和第3行的数据进行交换。
练习08:定义一个3行4列的二维数组,逐个从键盘输入值,编写程序将四周的数据清0。
练习09:定义一个N*N二维数组,从键盘上输入值,找出每行中最大值组成一个一维数组并输出。
练习10:从键盘上输入一个 3*4 的整型数组,求出其中的最大值和最小值,并显示出。
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int[] nums=new int[10];
for (int i = 0; i < nums.length; i++) {
System.out.println("请输入第"+(i+1)+"个整数");
nums[i]=sc.nextInt();
}
for (int i = 0; i < nums.length-1; i++) {
for (int j = 0; j < nums.length-1-i; j++) {
if (nums[j]>nums[j+1]) {
int temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
// System.out.println(Arrays.toString(nums));
System.out.println("从小到大输出:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i]+" ");
}
sc.close();
}
* <提示>程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);int[][] nums = new int[3][3];
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println("请输入数组中nums[" + i + "][" + j + "]" + "的值:");
nums[i][j] = sc.nextInt();
}
}System.out.println("您输入的二维数组元素是:");
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.print(nums[i][j] + " ");
}
System.out.println();
}int sum = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
if (i == j && i + j != 2 || i + j == 2) {
sum += nums[i][j];
}
}
}
System.out.println("您输入的二维数组对角线元素之和是:" + sum);sc.close();
}
public static void main(String[] args) {
// 已经按升序排好序的数组
int[] nums = new int[] { 11, 22, 33, 44, 55 };
// 新建一个比已经排好序的数组长度长1的数组
int[] newNums = Arrays.copyOf(nums, nums.length + 1);
System.out.println("数组插入元素前:");
for (int i = 0; i < newNums.length; i++) {
System.out.print(newNums[i] + " ");
}Scanner sc = new Scanner(System.in);
System.out.println("\n请输入你要插入的元素值(整数):");
int insert = sc.nextInt();
//定义插入元素的下标
int index = newNums.length - 1;for (int i = 0; i < newNums.length; i++) {
if (insert < newNums[i]) {
index = i;//获取插入元素的下标
break;
}
}//移动元素
for (int i = newNums.length - 1; i > index; i--) {
newNums[i] = newNums[i - 1];
}//将插入的元素存到插入位置上
newNums[index] = insert;System.out.println("数组插入元素后:");
for (int i = 0; i < newNums.length; i++) {
System.out.print(newNums[i] + " ");
}sc.close();
}
public static void main(String[] args) {
int[] nums = new int[] { 12, 56, 24, 78, 33, 92 };
System.out.println("逆序输出前:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}System.out.println("\n逆序输出:");
for (int i = nums.length - 1; i >= 0; i--) {
System.out.print(nums[i] + " ");
}}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想创建的数组长度:");
int length = sc.nextInt();
int[] nums = new int[length];for (int i = 0; i < nums.length; i++) {
System.out.println("请输入数组中的第" + (i + 1) + "个元素的值:");
nums[i] = sc.nextInt();
}System.out.println("您输入的数组是:" + Arrays.toString(nums));
int max = nums[0];
int maxIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > max) {
max = nums[i];
maxIndex = i;
}
}
int temp1 = nums[0];
nums[0] = max;
nums[maxIndex] = temp1;int min = nums[0];
int minIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
minIndex = i;
}
}
int temp2 = nums[nums.length - 1];
nums[nums.length - 1] = min;
nums[minIndex] = temp2;System.out.println("交换完数据后的数组是:" + Arrays.toString(nums));
sc.close();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数组的长度:");
int n = sc.nextInt();
int[] nums = new int[n];for (int i = 0; i < nums.length; i++) {
System.out.println("请输入数组中的第" + (i + 1) + "个数:");
nums[i] = sc.nextInt();
}
System.out.print("您输入的数组为:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
System.out.print("\n请输入向后移动的位数:");
int m = sc.nextInt();
//创建长度为m的数组,用来存储数组nums中后面的m个元素
int[] newNums = new int[m];
for (int i = 0; i < m; i++) {
newNums[i] = nums[n - m + i];
}
for (int i = n - 1; i >= m; i--) {
nums[i] = nums[i - m];
}
for (int i = 0; i < m; i++) {
nums[i] = newNums[i];
}
System.out.print("位移后的数组是:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}sc.close();
}
public static void main(String[] args) {
int[][] nums = new int[4][4];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println("请输入二维数组里第" + (i + 1) + "个元素里的第" + (j + 1) + "个值:");
nums[i][j] = sc.nextInt();
}
}System.out.println("您输入的二维数组是:");
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
//第一行数据和第四行数据交换
if (i == 0) {
int temp = nums[i][j];
nums[i][j] = nums[3][j];
nums[3][j] = temp;
//第二行数据和第三行数据交换
} else if (i == 1) {
int temp = nums[i][j];
nums[i][j] = nums[2][j];
nums[2][j] = temp;
}
}
}System.out.println("\n交换后的二维数组是:");
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}sc.close();
}
public static void main(String[] args) {
int[][] nums = new int[3][4];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println("请输入二维数组里第" + (i + 1) + "个元素里的第" + (j + 1) + "个值:");
nums[i][j] = sc.nextInt();
}
}System.out.println("您输入的二维数组是:");
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
if ((i == 0) || (i == 2) || (i == 1 && j == 0) || (i == 1 & j == 3)) {
nums[i][j] = 0;
}
}
}System.out.println("\n四周的数据清0后数组是:");
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}sc.close();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要创建的数组长度:");
int length = sc.nextInt();int[][] nums = new int[length][length];
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println("请输入二维数组中第" + (i + 1) + "个元素的第" + (j + 1) + "个值:");
nums[i][j] = sc.nextInt();
}
}System.out.println("您输入的数组是:");
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}int[] newNums = new int[length];
for (int i = 0; i < nums.length; i++) {
int max = nums[i][0];
for (int j = 0; j < nums[i].length; j++) {
if (nums[i][j] > max) {
max = nums[i][j];
}
}
newNums[i] = max;
}System.out.println("每行中最大值组成的一个一维数组是:");
System.out.println(Arrays.toString(newNums));sc.close();
}
public static void main(String[] args) {
int[][] nums = new int[3][4];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
System.out.println("请输入二维数组里第" + (i + 1) + "个元素里的第" + (j + 1) + "个值:");
nums[i][j] = sc.nextInt();
}
}System.out.println("您输入的二维数组是:");
for (int[] is : nums) {
for (int i : is) {
System.out.print(i + " ");
}
System.out.println();
}//假设二维数组里的第一个元素(一维数组)里的第一个值为最大值/最小值
int max = nums[0][0];
int min = nums[0][0];for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
if (nums[i][j] > max) {
max = nums[i][j];
}if (nums[i][j] < min) {
min = nums[i][j];
}
}
}System.out.println("数组中的最大值是:" + max);
System.out.println("数组中的最小值是:" + min);sc.close();
}