1、数组声明创建
a、数组
相同类型数据的有序集合。数组中元素可以通过下标访问。java中数组下标从0开始。
b、数组特点
长度固定;
元素类型必须相同;
元素类型可以为基本数据类型,也可以为引用类型;
数组变量属于引用类型,数组也可以看成对象,数组中每个元素相当于该对象成员变量。
基本类型和引用类型:
基本类型:计算机直接存储变量值。包含byte、short、int、long、float、double、char、boolean;
引用类型:计算机存储的是变量地址。类数组等;除了基本类型都说引用类型。
c、声明和创建
public class Demo01 {
public static void main(String[] args) {
// 变量类型 变量名称 = 变量值;
// 数组声明
int[] nums = {}; // 使用这个
int nums1[] = {};
// 创建了一个大小为10的int类型数组
int[] nums2 = new int[10];
// 数组.length 获取长度
System.out.println(nums2.length);
// 赋值 && 访问 通过下标
nums2[0] = 2;
System.out.println(nums2[0]);
int sum = 0;
for (int i = 0; i < nums2.length; i++) {
nums2[i] = i;
}
for (int i = 0; i < nums2.length; i++) {
sum += nums2[i];
}
System.out.println("数组求和结果"+sum);
}
}
d、数组初始化方式
静态初始化
动态初始化
public class Demo02 {
public static void main(String[] args) {
// 静态初始化,创建+赋值
int[] nums1 = {1, 2, 3};
// 动态初始化, 先创建再赋值,包含默认初始化
// 数组是引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个元素也被按照实 例变量同样的方式被隐式初始化。
int[] nums2 = new int[3];
// 其余元素被隐士初始化为0
nums2[0] = 1;
}
}
2、数组使用
a、foreach循环
b、数组作为方法入参
c、数组作为返回值
package Day02;
public class Demo03 {
public static void main(String[] args) {
int[] nums = new int[10];
for (int i = 0; i < nums.length; i++) {
nums[i] = i;
}
// 增强循环 快捷键 nums.for, 娶不到下标,适合访问数组元素
for (int num : nums) {
System.out.println(num);
}
System.out.println("=========================================");
printArray(nums);
System.out.println("=========================================");
printArray(reverseArray(nums));
}
// 打印数组,数组作为函数入参
public static void printArray(int[] arrays) {
for (int array : arrays) {
System.out.println(array);
}
}
// 反转数组,数组作为返回值
public static int[] reverseArray(int[] array) {
int len = array.length;
int [] result = new int[len];
for (int i = 0; i < len ; i++) {
result[len - i - 1] = array[i];
}
return result;
}
}
3、多维数组
/**
* 多维数组
*/
public class Demo04 {
public static void main(String[] args) {
// 声明和创建
int[][] nums = {{2,3}, {3, 4}, {5, 6, 7}};
// 访问方法
for (int i = 0; i < nums.length; i++) {
System.out.println("=======================");
for (int j = 0; j < nums[i].length; j++) {
System.out.println(nums[i][j]);
}
}
}
}
4、Arrays类
数组工具类 java.util.Arrays
package Day02;
import java.util.Arrays;
import java.util.Objects;
import java.util.StringJoiner;
/**
* Arrays工具类学习
*
* 注意: arrays类中的方法都是static类型的,因此使用的时候不用创建对象
*/
public class Demo05 {
public static void main(String[] args) {
int[] a = {2, 8, 0, 77777, 0};
// 打印的是hashCode
System.out.println(a);
System.out.println("===================");
System.out.println(Arrays.toString(a));
System.out.println("===================");
System.out.println(arrayToString(a));
System.out.println("===================");
// 内部实现,快排,原地改变a, 默认升序
Arrays.sort(a);
System.out.println(Arrays.toString(a));
/**
* toString方法源码: 遍历数组元素,讲每个元素拼接, 并且收尾添加括号,实际上也可以自己实现,但是不建议重复造轮子
* public static String toString(int[] a) {
* if (a == null)
* return "null";
* int iMax = a.length - 1;
* if (iMax == -1)
* return "[]";
*
* StringBuilder b = new StringBuilder();
* b.append('[');
* for (int i = 0; ; i++) {
* b.append(a[i]);
* if (i == iMax)
* return b.append(']').toString();
* b.append(", ");
* }
* }
*/
// 填充数组
Arrays.fill(a, 0);
/**
* 源码
* public static void fill(int[] a, int val) {
* for (int i = 0, len = a.length; i < len; i++)
* a[i] = val;
* }
* 还有一些重载的方法,比如填充固定范围内的值
* public static void fill(int[] a, int fromIndex, int toIndex, int val) {
* rangeCheck(a.length, fromIndex, toIndex);
* for (int i = fromIndex; i < toIndex; i++)
* a[i] = val;
* }
* 这里先对范围进行了校验
* private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
* if (fromIndex > toIndex) {
* throw new IllegalArgumentException(
* "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
* }
* if (fromIndex < 0) {
* throw new ArrayIndexOutOfBoundsException(fromIndex);
* }
* if (toIndex > arrayLength) {
* throw new ArrayIndexOutOfBoundsException(toIndex);
* }
* }
*/
System.out.println("===================");
System.out.println(arrayToString(a));
// 二分查找,使用二分查找必须先排序
int[] b = {1,2,323,23,543,12,59};
Arrays.sort(b);
System.out.println("===================");
System.out.println("该元素的索引:"+Arrays.binarySearch(b, 12));
// 转换为list, stream
// 返回list
System.out.println(Arrays.asList(b));
// 返回stream
System.out.println(Arrays.stream(b));
}
/**
* 自己实现的toString
*
* @param a 输入数组
* @return string
*/
public static String arrayToString(int[] a) {
if (Objects.isNull(a)) {
return null;
}
if (a.length == 0) {
return "[]";
}
StringBuilder result = new StringBuilder("[");
for (int i = 0; i < a.length; i++) {
if (i == a.length - 1) {
result.append(a[i]);
} else {
result.append(a[i]).append(",").append(" ");
}
}
result.append("]");
return result.toString();
}
}
5、排序算法
package Day02;
import java.util.Arrays;
/**
* 冒泡排序
* 1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
* 2. 对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会
* 是最大的
* 数。
* 3. 针对所有的元素重复以上的步骤,除了最后一个。
* 4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
*
* @since 2022/08/11
*/
public class Demo06 {
public static void main(String[] args) {
int[] a = {3,5,1,9,7};
sort(a);
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a) {
// 走几次
for (int i = 0; i < a.length - 1; i++) {
// 每次进行了多少次两两比较
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j+1] < a[j]) {
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
}