## 数组
![image-20230101170812281](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230101170812281.png)
![image-20230101171524206](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230101171524206.png)
![image-20230101174610907](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230101174610907.png)
java.lang.ArrayIndexOutOfBoundsException: 10
![image-20230101204301343](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230101204301343.png)
![image-20230101223223711](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230101223223711.png)
![image-20230101223238748](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230101223238748.png)
基本数据类型的数值存放在哪里???疑问
![image-20230102132907044](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230102132907044.png)
![image-20230102133811891](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230102133811891.png)
![image-20230102141901697](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230102141901697.png)
![image-20230102154719422](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230102154719422.png)
![image-20230102155456278](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230102155456278.png)
***直接打印数组本身的话,就会显示该数组的内存地址***
![image-20230102170646204](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230102170646204.png)
Java中的fill可以单独填充某一个元素吗????
![image-20230104230902136](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230104230902136.png)
![image-20230105190815703](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230105190815703.png)
![image-20230105191014410](C:\Users\biao\AppData\Roaming\Typora\typora-user-images\image-20230105191014410.png)
package com.fang.array;
public class ArrayDemo01 {
public static void main(String[] args) {
int[] nums;//1.声明一个数组
nums = new int[10];//2.创建一个数组
// int[] nums2 = new int[10];
//3.给数组元素中赋值
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
nums[4] = 5;
nums[5] = 6;
nums[6] = 7;
nums[7] = 8;
nums[8] = 9;
nums[9] = 10;
//计算所有元素的和
int sum = 0;
//获取数组长度:arrays.length
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
}
System.out.println("总数为:"+sum);
}
}
package com.fang.array;
public class ArrayDemo02 {
public static void main(String[] args) {
//静态初始化:创建+赋值
int[] a = {1,2,3,4,5,6,7,8};
System.out.println(a[0]);
//动态初始化:包含默认初始化
int[] b = new int[10];
b[0] = 10;
System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(b[2]);
System.out.println(b[3]);
}
}
package com.fang.array;
public class ArrayDemo03 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
//打印全部的数组元素
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
System.out.println("==========================");
//计算所有元素的和
int sum = 0;
for (int i = 0; i < arrays.length; i++) {
sum = sum + arrays[i];
}
System.out.println("sum="+sum);
System.out.println("==========================");
//查找最大元素
int max = arrays[0];
for (int i = 1; i < arrays.length; i++) {
if (arrays[i]>max){
max = arrays[i];
}
}
System.out.println("最大的数为:"+max);
}
}
package com.fang.array;
public class ArrayDemo04 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
//JDK1.5,没有下标
// for (int array : arrays) {
// System.out.println(array);
// }
// printArray(arrays);
int[] reverse = reverse(arrays);
printArray(reverse);
}
//反转数组
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
//反转的操作
for (int i = 0, j = result.length-1; i < arrays.length; i++,j--) {
result[j] = arrays[i];
}
return result;
}
//打印数组元素
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
}
}
package com.fang.array;
public class ArrayDemo05 {
public static void main(String[] args) {
//[4][2] 4行2列
/*
1,2 array[0]
2,3 array[1]
3,4 array[2]
4,5 array[3]
*/
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
// System.out.println(array[0]);//直接打印数组本身的话,就会显示该数组的内存地址
// System.out.println(array[0][0]);
printArray(array[0]);
System.out.println("==============");
System.out.println(array[1][1]);
System.out.println("==============");
System.out.println(array.length);
System.out.println(array[0].length);
System.out.println("==============");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
}
//打印数组元素
public static void printArray(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
}
}
package com.fang.array;
import java.util.Arrays;
public class ArrayDemo06 {
public static void main(String[] args) {
int[] a = {2121,21432,3435,5435,4353425,5432632};
// System.out.println(a);//[I@1b6d3586
//
// //打印数组元素Arrays.toString
// System.out.println(Arrays.toString(a));
// printArray(a);
Arrays.sort(a);//数组进行排序:升序
System.out.println(Arrays.toString(a));
Arrays.fill(a,0);//数组填充
// Arrays.fill(a,2,5,0);//数组a中2~5之间的元素为0
System.out.println(Arrays.toString(a));
}
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
if (i == 0){
System.out.print("[");
}
if(i == arrays.length-1){
System.out.print(arrays[i]+"]");
}else {
System.out.print(arrays[i]+", ");
}
}
}
}
package com.fang.array;
import java.util.Arrays;
public class ArrayDemo07 {
public static void main(String[] args) {
int[] a = {23,2321,3,21,3,123,12,43,124};
int[] b = sort(a);//调用完我们自己写的排序方法以后,返回一个排序后的数组
System.out.println(Arrays.toString(b));
}
//冒泡排序
//1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
//2.每一次比较,都会产生出一个最大,或者最小的数字
//3.下一轮则可以少一次排序
//4.依次循环,直到结束
public static int[] sort(int[] array){
//临时变量
int temp = 0;
//外层循环,判断我们这个要走多少次;
for (int i = 0; i < array.length-1; i++) {
boolean flag = false;//通过flag标识为减少没有意义的比较;假设有100个元素,当循环到55次的时候数字没有变动,就省下了45次遍历检查的时间
//内层循环,比较判断两个数,如果第一个数,比第二个数大,则交换位置
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 == false){
break;
}
}
return array;
}
}
package com.fang.array;标签:int,image,System,day15,println,array,out From: https://www.cnblogs.com/biao3/p/17033367.html
import java.util.Arrays;
public class ArrayDemo08 {
public static void main(String[] args) {
//1.创建一个二维数组11*11 0:没有棋子 1.黑棋 2.白棋
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();
}
System.out.println("========================");
//转换为稀疏数组保存
//获取有效值的个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; 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) {
// for (int anInt : ints) {
// System.out.print(anInt+"\t");
// }
// System.out.println();
// }
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t");
}
System.out.println("======================");
System.out.println("还原");
//1.读取稀疏数组
int[][] array3 = new int[array2[0][0]][array2[0][1]];
//2.给其中的元素还原它的值
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
//3。打印
System.out.println("输出还原的数组");
for (int[] ints : array3) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}