数组知识点
package day3; public class Demo01 { public static void main(String[] args) { // int a[]; //1:声明数组 // a = new int[5]; //2:分配空间 【数据类型[] 数组名 = new 数据类型[大小]】; // a[0] = 5; //3:赋值 // a[0] *= 5; //4:对数组进行处理 // System.out.println(a[0]); // 数组长度已经固定 int score[] = { 1, 2, 3, 4, 5 }; // score[5] = 6; //数组越界 int score2[] = new int[] { 1, 2, 3, 4, 5 }; score2[3] = 55; System.out.println(score2.length); } }
package day3; public class Demo02 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("本次活动特价商品有:"); String good[] = { "nike", "adidas", "Kappa" }; for (int i = 0; i < good.length; i++) { System.out.println(good[i]); } } }
运行图
package day3; import java.util.Scanner; public class Demo03 { public static void main(String[] args) { Scanner in = new Scanner(System.in); double money[] = new double[5]; double sum = 0.0; for (int i = 0; i < 5; i++) { System.out.println("请输入第" + (i + 1) + "笔购物金额:"); money[i] = in.nextDouble(); sum += money[i]; } System.out.println("序号 金额(元)"); for (int i = 0; i < money.length; i++) { System.out.println((i + 1) + " " + money[i]); } System.out.println("金额总和为" + sum); } }
运行图
依次输入1、2、3、4、5
练习、求平均值
package day3; import java.util.Scanner; public class Lianxi { public static void main(String[] args) { // TODO Auto-generated method stub int scores[] = new int[5]; int sum = 0; Scanner in = new Scanner(System.in); System.out.println("5个成绩"); int num = scores.length; for(int i = 0;i<num;i++){ scores[i] = in.nextInt(); sum += scores[i]; } System.out.println("平均分是"+(double)sum/scores.length); } }
package day3; import java.util.Scanner; public class Demo04 { public static void main(String[] args) { // TODO Auto-generated method stub int price[] = new int[5]; Scanner in = new Scanner(System.in); for (int i = 0; i < price.length; i++) { System.out.println("请输入第" + (i + 1) + "家手机价格"); price[i] = in.nextInt(); } int min = price[0]; for (int i = 0; i < price.length; i++) { if (price[i] > min) { min = min; } else { min = price[i]; } } System.out.println(min); } }
运行图
依次输入1、2、3、4、5取得最小值
foreach循环
package day3; public class Demo05 { public static void main(String[] args) { int arr[] = { 12, 34, 56, 99, 78 }; // foreach循环 i为数组数据 for (int i : arr) { System.out.println(i); } } }
数据类型传值
package day3; public class Demo06 { public static void main(String[] args) { // TODO Auto-generated method stub // 基本数据类型传值 /*int i = 6; int j = i; i = 7; System.out.println(j);*/ // 引用数据类型 int zhangSan[] = {180,25}; int liSi[] = zhangSan; zhangSan[0] = 170; System.out.println(liSi[0]);//结果是170而不是180 ,因为是数组,他们公用一个地址,调用的也是数组地址上的数据, } }
sort升序排列
package day3; import java.util.ArrayList; import java.util.Arrays; public class Demo07 { public static void main(String[] args) { int arr[] = {99,98,75,45,76,55}; //sort:对数组升序排列 Arrays.sort(arr); for (int i : arr) { System.out.println(i); } } }
运行图
比较两个数组相等的equal函数
package day3; import java.util.Arrays; public class Demo08 { public static void main(String[] args) { // 比较两个数组是否完全相等 int arr1[] = {10,20,30,40}; int arr2[] = {10,20,30,40}; int arr3[] = {10,20,30}; int arr4[] = {20,10,30,40}; // 数组的相等是指数组内元素大小、顺序和类型全部相等 System.out.println(Arrays.equals(arr1, arr2)); System.out.println(Arrays.equals(arr2, arr3)); System.out.println(Arrays.equals(arr2, arr4)); } }
运行图
toString函数:把数组转换成字符串
package day3; import java.util.Arrays; public class Demo09 { public static void main(String[] args) { // TODO Auto-generated method stub int arr1[] = {10,50,40,30}; //toString:用来吧数组转换成字符串 System.out.println(Arrays.toString(arr1)); } }
fill:给源数组填充新数据
package day3; import java.util.Arrays; public class Demo10 { public static void main(String[] args) { int arr1[] = {10,50,40,30}; //fill:用来给源数组填充新数据 Arrays.fill(arr1, 30); //转换成字符串 System.out.println(Arrays.toString(arr1)); } }
运行图
binarySearch:查找数组中指定数据的索引
package day3; import java.util.Arrays; public class Demo11 { public static void main(String[] args) { //binarySearch对升序排列的数组才有效 //binarySearch:用来查找数组中指定数据的索引 int arr[] = {10,20,50,40,30,55,20}; Arrays.sort(arr); //排序 System.out.println(Arrays.toString(arr)); System.out.println(Arrays.binarySearch(arr, 20)); } }
copyOf:复制数组
package day3; import java.util.Arrays; public class Demo12 { public static void main(String[] args) { // 复制数组 int[] arr = {10,50,40,30}; //copyOf:从数组头部开始,复制指定长度的数组 //参数1:要复制的源数组 //参数2:要复制的数组个数 //注意如果要复制的数组元素数量大于源数组长度,多出的数组元素会补0 int[] arr2 = Arrays.copyOf(arr, 3); System.out.println(Arrays.toString(arr2)); int arr3[] = Arrays.copyOf(arr, 4); System.out.println(Arrays.toString(arr3)); int arr4[] = Arrays.copyOf(arr, 6); System.out.println(Arrays.toString(arr4)); } }
运行图
copyOfRange:复制数组中的某一段数据
package day3; import java.util.Arrays; public class Demo13 { public static void main(String[] args) { //copyOfRange:复制数组中的某一段数据 //参数1:要复制的源数组; //参数2:开始索引(包括) //参数3:结束索引(不包括) int arr[] = {10,20,33,44,66,77}; int arr2[] = Arrays.copyOfRange(arr, 0, 3); //or //int arr2[] = Arrays.copyOfRange(arr, 0, arr.length-1); System.out.println(Arrays.toString(arr2)); } }
运行图
输出一段数组,并将数组中的元素升序排列、逆序排列
package day3; import java.util.Arrays; public class Demo14 { public static void main(String[] args) { char arr[] = {'a','c','u','b','f','c'}; System.out.println(Arrays.toString(arr)); Arrays.sort(arr); System.out.println(Arrays.toString(arr)); char newArr[] = new char[arr.length]; for (int i = 0; i < arr.length; i++) { newArr[i] = arr[arr.length-1-i]; } System.out.println(Arrays.toString(newArr)); } }