package Study;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class MyStudy1 {
/**
* 数组 一维数组声明方式:int a[],int[] a;声明时不能指定长度int a[5]
* 基本数据类型:数组初始值不同。引用数据类型则都是null。数组有length属性 多维数组 声明方式:int[][] arr = new
* int[3][3]; int[][] arr = new int[3][];不可以写成int[][] arr = new int[][3];
* int[][] arr = new int[][]{{1,2},{3,4}};
*/
public static void main(String[] args) {
// showA_Z();
// showA_Number();
// showFeb();
// showSort();
// showSum();
// showInsert();
// showDiff();
// showExch();
// showMove();
// showMulExch();
// showZero();
showMax();
}
private static void showMax() {
// 定义一个N*N二维数组,从键盘上输入值,找出每行中最大值组成一个一维数组并输出;
int[][] a = { { 1, 2, 3, 4 }, { 5, 6, 9, 8 }, { 9, 10, 18, 12 }, { 13, 14, 15, 16 } };
int[] c = new int[4];int m = 0;
for (int i = 0; i < a.length; i++) {
int max = a[i][0];
System.out.println(max);
for (int j = 0; j < a[i].length; j++) {
if (max < a[i][j])
max = a[i][j];
m++;
if (m % 4 == 0)
c[i] = max;
}
}
System.out.println(Arrays.toString(c));
}
private static void showZero() {
// 定义一个3行4列的二维数组,逐个从键盘输入值,将左下三角的值清0
int[][] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (j <= i)
a[i][j] = 0;
}
System.out.println();
}
System.out.println("清零后:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
private static void showMulExch() {
// 定义一个4行4列的二维数组,逐个从键盘输入值,然后将第1行和第4行的数据进行交换,将第2行和第3行的数据进行交换
int[][] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
int b = 0;
System.out.println("交换前:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
System.out.println("交换后:");
for (int j = 0; j < 4; j++) {
b = a[0][j];// 1 2 3 4
a[0][j] = a[a.length - 1][j];
a[a.length - 1][j] = b;
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
private static void showMove() {
// 有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
int[] a = { 1, 2, 3, 4, 5, 6 };
int m = 2, q = 0, k = 0;
int[] b = new int[m];
for (int j = 0; j < b.length; j++) {
b[j] = a[j];
System.out.print("b:" + b[j] + " ");// 1 2
}
for (int i = m; i < a.length; i++) {
a[k] = a[i];
k++;
System.out.print(k + " ");
}
for (int n = m; n > 0; n--) {
a[a.length - n] = b[q];
q++;
if (q >= m)
break;
}
System.out.println();
for (int w = 0; w < a.length; w++)
System.out.println(a[w]);
}
private static void showExch() {
// 输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
int[] a = { -1, -2, 3, 4, 5, 7, 6 };// 7,-1,3,4,6,-2
int min = a[0], max = a[0];
int m = 0, n = 0;// 1 5
for (int i = 0; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
m = i;
}
if (a[i] > max) {
max = a[i];
n = i;
}
}
System.out.println(max + " " + n + " " + min + " " + m);
int temp1 = a[0], temp2 = a[a.length - 1];
a[0] = max;
a[n] = temp1;
a[a.length - 1] = min;
a[m] = temp2;
for (int j = 0; j < a.length; j++) {
System.out.println(a[j]);
}
}
private static void showDiff() {
// 利用随机数生成一个整数数组,数组中有10个元素,每个元素的值都在1-30之间,且要求各个数值不能相同。打印该数组。
int[] a = new int[10];
Set set = new HashSet<>();
int i = 0;
while (true) {
int random = (int) (Math.random() * 10 + 1);
set.add(random);
if (set.size() == 10)
break;
}
for (Integer integer : set) {
a[i] = integer;
i++;
}
for (int j = 0; j < a.length; j++)
System.out.println(a[j]);
}
private static void showInsert() {
// 有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
int[] a = { 1, 2, 3, 4, 6, 7 };
int[] b = new int[a.length + 1];
int c = 5;
b = Arrays.copyOf(a, b.length);
for (int i = a.length - 1; i >= 0; i--) {
if (c < b[i])
b[i + 1] = b[i];
else {
b[i + 1] = c;
break;
}
}
for (int k = 0; k < b.length; k++)
System.out.print(b[k] + " ");
}
private static void showSum() {
// 求一个3*3矩阵对角线元素之和
/**
* 88 33 167 24 48 191 158 151 61
*/
int[][] a = new int[3][3];
int b = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = (int) ((Math.random() * 9 + 1) * 20);
System.out.print(a[i][j] + " ");
if (i == j)
b += a[i][i];
}
}
System.out.print("\n" + b);
}
private static void showSort() {
// 对10个整数进行按照从小到大的顺序排序
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = (int) ((Math.random() * 9 + 1) * 10000);
System.out.print(a[i] + " ");
}
System.out.println();
for (int j = 0; j < a.length; j++) {
for (int k = 0 + j; k < a.length; k++) {
if (a[j] < a[k]) {
int temp = a[j];
a[j] = a[k];
a[k] = temp;
}
}
}
for (int m = 0; m < a.length; m++)
System.out.print(a[m] + " ");
}
private static void showFeb() {
// 用数组求出斐波那契数列的前20项值
int[] a = new int[20];
for (int i = 0; i < 20; i++) {
if (i == 0 || i == 1) {
a[i] = i + 1;
} else {
a[i] = a[i - 1] + a[i - 2];
}
System.out.print(a[i] + " ");
if (i % 5 == 0)
System.out.println();
}
}
private static void showA_Number() {
/*
* 创建一个char类型的36个元素的数组,前26个元素放置'A'-'Z', 后10个元素放置'0'-'9'。 使用for循环访问所有元素并打印出来。
* 提示:char类型数据运算 'A'+1 -> 'B','0'+1 -> '1'
*/
char[] a = new char[36];
for (int i = 0; i < a.length; i++) {
if (i < 26)
a[i] = (char) (i + 65);
else
a[i] = (char) (i - 26 + '0');
}
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
if (j % 5 == 0)
System.out.println();
}
}
private static void showA_Z() {
/*
* 创建一个char类型的26个元素的数组,分别 放置'A'-'Z'。 使用for循环访问所有元素并打印出来。 小提示 A 65,a 97
*/
char[] a = new char[26];
for (int i = 0; i < a.length; i++) {
a[i] = (char) (i + 65);
}
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
if (j % 5 == 0)
System.out.println();
}
}
}