JAVA数组小记
数组的定义
int[] arrays;
arrays = new int[];
FOR EACH循环
for (int array : arrays){
}//一维数组
Arrays类
Arrays.toString();//一维数组
for (int[] ints : array2) {//二维数组
System.out.println(Arrays.toString(ints));}
冒泡排序
public class Maopao {
public static void main(String[] args) {
int[] a = {1,3,1,22,13,5};
int[] so = sort(a);
System.out.println(Arrays.toString(so));
}
public static int[] sort(int[] array){
int temp = 0;
for (int i = 0; i < array.length; i++) {
boolean flag = false;//优化最后一次循环
for (int j = 0; j < array.length-1-i; j++) {
if (array[j] > array[j+1]){
temp =array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag = true;
}
}
if (!flag){
break;
}
}
return array;
}
}
稀疏数组
-
定义
稀疏数组可以看做是普通数组的压缩,但是这里说的普通数组是值无效数据量远大于有效数据量的数组形如:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
其稀疏数组形式:
11 11 2
1 2 1
2 3 2
- 代码如下
public class XiShu {
public static void main(String[] args) {
//1. 创建二维数组
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
System.out.println("输出原始数组");
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
//2. 转换为稀疏数组
int sum = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
if (array1[i][j]!=0){
sum++;
}
}
}
System.out.println("有效的数值有"+sum+"个");
int[][] array2 = new int[sum+1][3];
array2[0][0] = 11;
array2[0][1] = 11;
array2[0][2] = sum;
int count = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
if (array1[i][j]!=0){
count++;
array2[count][0] = i;
array2[count][1] = j;
array2[count][2] = array1[i][j];
}
}
}
System.out.println("输出稀疏数组");
for (int[] ints : array2) {
System.out.println(Arrays.toString(ints));
}
//3. 将稀疏数组还原为原数组
System.out.println("输出原数组");
int[][] array3 = new int[array2[0][0]][array2[0][1]];
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
for (int[] ints : array3) {
System.out.println(Arrays.toString(ints));
}
}
}
(random)用法
double value = Math.random();[0.0,1.0]
int value = (int)(Math.random()*10+5);[5,10]
求质数
public class PrimeNumber {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean flag = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0){
flag = false;
break;
}
}
if (flag)
System.out.print(i+"为质数"+"\t");
}
}
}
回型数的矩阵实现
public class Rectangle {
public static void main(String[] args) {
System.out.println("输入矩阵边长:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] arr =new int[n][n];
int count = 0;
int maxX = n - 1;
int maxY = n - 1;
int minX = 0;
int minY = 0;
while (minX <= maxX){
for (int i = minX; i <= maxX ; i++) {
arr[minY][i] = ++count;
}
minY++;
for (int i = minY; i <= maxY; i++) {
arr[i][maxX] = ++count;
}
maxX--;
for (int i = maxX; i >=minX ; i--) {
arr[maxY][i] = ++count;
}
maxY--;
for (int i = maxY; i >= minY; i--) {
arr[i][minX] = ++count;
}
minX++;
}
for (int[] ints : arr) {
System.out.println(Arrays.toString(ints));
}
}
}
快速排序
public class QucikSort {
public static void main(String[] args) {
int[] arr = new int[]{5,1,3,8,4,7,2,6,9};
quick(arr,0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
public static void quick(int[] arr,int left,int right){
if (arr == null || arr.length ==0){
return;
}
if (left > right){
return;
}
int key = arr[left];
int l = left;
int r = right;
while (l != r){
while (arr[r] >= key && l <r){
r--;
}
while (arr[l] <= key && l <r) {
l++;
}
if (l < r){
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}
arr[left] = arr[l];
arr[l] = key;
quick(arr,left,l-1);
quick(arr,l+1,right);
}
}