1. 概念
2. 创建方式
package com.qfedu.test1;
/**
* 创建数组的四种方式
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
// 方式1 先声明 再分配空间
int [] arr1;
arr1 = new int[6];
// 方式2 连声明 带分配空间
int [] arr2 = new int[10];
// 方式3 声明并且赋值(繁琐)
int [] arr3 = new int[] {10,20,30,50};
// 方式4 声明并且赋值(简单)
int [] arr4 = {1,2,3,4,5};
}
}
package com.qfedu.test1;
/**
* 各种类型数组写法
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
byte [] b1 = new byte[10];
short [] s1 = {1,2,3,4,5};
int [] i1 = new int[] {1,2,3,4,5};
long [] l1 = new long[20];
float [] f1 = new float[5];
double [] d1 = new double[2];
boolean [] bl1 = {true,false,true,false,false};
char [] ch1 = {'a','b',65,66,67,68,'\u4e2d'};
String [] strs = new String[] {"hello","hhh","heiheihei","中文","汉语"};
}
}
3. 下标和访问
package com.qfedu.test2;
/**
* 数组的访问:赋值和取值统称为数组的访问
* 访问数组中的元素通过下标:下标从0开始 ,依次+1
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
int [] arr1 = new int[5];
// 存值
arr1[0] = 11;
arr1[1] = 22;
arr1[2] = 33;
arr1[3] = 44;
arr1[4] = 55;
// arr1[5] = 66; 访问不存在的下标 都将出现数组下标越界异常 ArrayIndexOutOfBoundsException
// 取值
System.out.println(arr1[0]);
System.out.println(arr1[1]);
System.out.println(arr1[2]);
System.out.println(arr1[3]);
System.out.println(arr1[4]);
// System.out.println(arr1[6]); 访问不存在的下标 都将出现数组下标越界异常 ArrayIndexOutOfBoundsException
}
}
4. 遍历
package com.qfedu.test2;
/**
* 使用for循环遍历数组
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
int [] arr1 = {11,22,33,44,55,66};
System.out.println(arr1[0]);
System.out.println(arr1[1]);
System.out.println(arr1[2]);
System.out.println(arr1[3]);
System.out.println(arr1[4]);
System.out.println(arr1[5]);
System.out.println("----------------------------------------");
for(int i = 0;i < 6;i++) {
System.out.println(arr1[i]);
}
}
}
package com.qfedu.test2;
/**
* 通过for循环遍历数组取值
* 数组的属性 length 表示数组的长度 是一个int类型的整数 通过数组名.length 来使用
* @author WHD
*
*/
public class Test3 {
public static void main(String[] args) {
int [] arr1 = {1,2,3,4,5,6,4556,78,12,475,12,45,12,454,21,454,57,211,547,87,12,45};
System.out.println(arr1.length);
for(int i = 0;i < arr1.length;i++) {
System.out.println(arr1[i]);
}
}
}
package com.qfedu.test2;
import java.util.Scanner;
/**
* 通过循环给数组赋值
* @author WHD
*
*/
public class Test4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int [] arr1 = new int[6];
for(int i = 0;i < arr1.length;i++) {
System.out.println("请输入第"+ (i + 1) +"个元素的值");
arr1[i] = input.nextInt();
}
System.out.println("-------------------------------------");
for (int i = 0; i < arr1.length; i++) {
System.out.println("通过循环取值:" + arr1[i]);
}
System.out.println("程序结束");
}
}
5.数组的默认值
package com.qfedu.test3;
/**
* 数组的默认值
* 整数:0
* 小数:0.0
* 布尔:false
* 字符:\u0000
* 其他引用数据类型:null
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
byte [] b1 = new byte[3];
for (int i = 0; i < b1.length; i++) {
System.out.print(b1[i] + "\t");
}
System.out.println();
short [] s1 = new short[5];
for (int i = 0; i < s1.length; i++) {
System.out.print(s1[i] + "\t");
}
System.out.println();
int [] i1 = new int[4];
for (int i = 0; i < i1.length; i++) {
System.out.print(i1[i] + "\t");
}
System.out.println();
long l1[] = new long[10];
for (int i = 0; i < l1.length; i++) {
System.out.print(l1[i] + "\t");
}
System.out.println();
float [] f1 = new float[2];
for (int i = 0; i < f1.length; i++) {
System.out.print(f1[i] + "\t");
}
System.out.println();
double [] d1 = new double[1];
for (int i = 0; i < d1.length; i++) {
System.out.print(d1[i] + "\t");
}
System.out.println();
char [] ch1 = new char[3];
for (int i = 0; i < ch1.length; i++) {
System.out.print(ch1[i] + "-");
}
System.out.println();
boolean [] bl1 = new boolean[4];
for (int i = 0; i < bl1.length; i++) {
System.out.print(bl1[i] + "\t");
}
System.out.println();
String [] strs = new String[5];
for (int i = 0; i < strs.length; i++) {
System.out.print(strs[i] + "\t");
}
System.out.println();
}
}
6. 数组的存储
7. 课堂练习
package com.qfedu.test4;
/**
* 求数组中的最大值 / 最小值
* 分析:
* 先假设一个最大值,然后依次与后续的元素来比较,
* 如果遇到比当前 元素还大值,则交换头衔,最后保留最大值头衔的
* 元素 就是我们最终需要的值
* @author WHD
*
*/
public class Test4 {
public static void main(String[] args) {
int [] nums = {567,4445,56,2300,12457,885,99632,22};
int min = nums[0];
for(int i = 0;i < nums.length; i ++) {
if(min > nums[i]) {
min = nums[i];
}
}
System.out.println("最小值是:" + min);
}
}
8.复制数组
8.1 方式1
package com.qfedu.test4;
/**
* 数组扩容(复制)
* 1.创建一个长度大于原数组的数组 然后依次将原数组的元素 复制 到新数组中
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
int [] nums1 = {11,22,33,44,55};
int [] nums2 = new int[nums1.length * 2];
for(int i = 0;i < nums1.length;i++) {
nums2[i] = nums1[i];
}
for(int i = 0;i < nums2.length;i++) {
System.out.print(nums2[i] + "\t");
}
System.out.println();
}
}
8.2 方式2
package com.qfedu.test5;
import java.util.Arrays;
/**
* 数组的复制
* 2.System.arraycopy(原数组,原数组起始,新数组,新数组起始,长度);
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
int [] oldArr = {1,2,3,4,5};
int [] newArr = new int[10];
System.arraycopy(oldArr, 4, newArr, 5, 1);
for (int i = 0; i < newArr.length; i++) {
System.out.print(newArr[i] + "\t");
}
System.out.println();
}
}
8.3 方式3
package com.qfedu.test5;
import java.util.Arrays;
/**
* 数组的复制
* 3.java.util.Arrays.copyOf(原数组, 新长度);//返回带有原值的新数组。
* @author WHD
*
*/
public class Test1 {
public static void main(String[] args) {
int [] nums = {1,2,3,4,5};
int [] nums1 = Arrays.copyOf(nums, 3);
for (int i = 0; i < nums1.length; i++) {
System.out.print(nums1[i] + "\t");
}
System.out.println();
System.out.println("-------------------------------");
int [] nums2 = Arrays.copyOf(nums, 6);
for (int i = 0; i < nums2.length; i++) {
System.out.print(nums2[i] + "\t");
}
System.out.println();
}
}
9. 面试题
值传递和引用传递的区别?
package com.qfedu.test6;
/**
* 面试题:Java中值传递和引用传递的区别?
* 值传递,传递的是值的副本,在方法中对值的操作不会影响原来的变量
* 引用传递,传递的是地址,根据这个地址对数据的操作,会影响原来的变量
* String类型是特殊的引用数据类型,作为参数传递不会影响原来的变量
* @author WHD
*
*/
public class Test2 {
public static void main(String[] args) {
int a = 10;
m1(a);
System.out.println(a);
System.out.println("----------------------------------------------");
int [] arr1 = {1,2,3,4,5};
System.out.println(arr1);
m2(arr1);
for (int i = 0; i < arr1.length; i++) {
System.out.print(arr1[i] + "\t");
}
}
public static void m2(int [] arr2) {
System.out.println(arr2);
for (int i = 0; i < arr2.length; i++) {
arr2[i] += 1;
}
}
public static void m1(int num) {
num+= 10;
}
}