-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
一、数组概念:
数组是存储同一种数据类型多个元素的集合。也可以看成是一个容器。
数组既可以存储基本数据类型,也可以存储引用数据类型。
二、数组的定义格式:
格式1:数据类型[] 数组名;
格式2:数据类型 数组名[];
Java中的数组必须先初始化,然后才能使用。
所谓初始化:就是为数组中的数组元素分配内存空间,并为每个数组元素赋值。
四、数组的初始化方式:
动态初始化:初始化时只指定数组长度,由系统为数组分配初始值。
格式:数据类型[] 数组名 = new 数据类型[数组长度];
数组长度其实就是数组中元素的个数。
举例: int[] arr = new int[3];
解释:定义了一个int类型的数组,这个数组中可以存放3个int类型的值。
静态初始化:初始化时指定每个数组元素的初始值,由系统决定数组长度。
格式:数据类型[] 数组名 = new 数据类型[]{元素1,元素2,…};
举例:int[] arr = new int[]{1,2,3};
解释:定义了一个int类型的数组,这个数组中可以存放3个int类型的值,并且值分别是1,2,3。
其实这种写法还有一个简化的写法 : int[] arr = {1,2,3};
五、数组的练习:
package cn.hebei.sjz_数组;
/*
* 练习1:一维数组的遍历
*/
public class Demo1 {
public static void main(String[] args) {
// 数组的遍历
int[] arr = { 2, 3, 4, 5, 6 };
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i]);
} else {
System.out.print(arr[i] + ",");
}
}
System.out.print("]");
}
}
package cn.hebei.sjz_数组;
/*
* 获取数组中的最大值和最小值
*/
public class Test1 {
public static void main(String[] args) {
int[] arr = { 23, 2, 4, 56, 22, 55, 464 };
// 定义最大值和最小值都为arr[0],
int min = arr[0];
int max = arr[0];
// 遍历数组
for (int i = 0; i < arr.length; i++) {
// 求最小值
if (arr[i] < min) {
min = arr[i];
}
// 求最大值
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("最大值是:" + max);
System.out.println("最小值是:" + min);
}
}
package cn.hebei.sjz_数组;
/*
* 数组逆序打印
*/
public class Test2 {
public static void main(String[] args) {
int[] arr = { 23, 34, 12, 24, 45, 43 };
// 数组逆序,逆序只需要一半的元素和另一半对调
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
// 数组打印
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i] + "]");
break;
}
System.out.print(arr[i] + ",");
}
}
}
package cn.hebei.sjz_数组;
import java.util.Scanner;
/*
* 根据键盘录入索引,查找对应星期(隐藏返回星期的需求)
*/
public class Test3 {
public static void main(String[] args) {
//变量定义
String[] week = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
Scanner sc = new Scanner(System.in);
System.out.println("请输入数字:");
int num = sc.nextInt();
//将不合法的排除掉,是程序更健壮
if (num > 7 || num < 1) {
System.out.println("输入不合法,请重新输入");
return;
}
String s = get(week, num);
System.out.println(s);
}
private static String get(String[] week, int num) {
int index = num - 1;
return week[index];
}
}
package cn.hebei.sjz_数组;
import java.util.Scanner;
/*
* 数组元素查找(查找指定元素第一次在数组中出现的索引)
*/
public class Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = { 12, 23, 34, 32, 454, 645 };
System.out.println("请输入数字:");
// 指定元素
int num = sc.nextInt();
// 定义索引
int index;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == num) {
index = i;
System.out.println(index);
}
}
}
}
定义格式:
数据类型[][] 变量名 = new 数据类型[m][n];
数据类型[][] 变量名 = new 数据类型[m][];
数据类型[][] 变量名 = new 数据类型[][]{{元素…},{元素…},{元素…}};
练习:
package cn.hebei.sjz_数组;
/*
* 二维数组的遍历
*/
public class Demo1 {
public static void main(String[] args) {
//二维数组的遍历
int[][] arr2 = {{2,3,5},{2},{2,3,4,5}};
System.out.print("[");
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[i].length; j++) {
if (j == arr2[i].length - 1 && i == arr2.length - 1) {
System.out.print(arr2[i][j]);
}else{
System.out.print(arr2[i][j] + ",");
}
}
}
System.out.print("]");
}
}
package cn.hebei.sjz_数组;
/*
* 公司年销售额求和
* 某公司按照季度和月份统计的数据如下:单位(万元)
* 第一季度:22,66,44
* 第二季度:77,33,88
* 第三季度:25,45,65
* 第四季度:11,66,99
*/
public class Test {
public static void main(String[] args) {
int[][] arr = { { 22, 66, 44 }, { 77, 33, 88 }, { 25, 45, 65 },
{ 11, 66, 99 } };
// 遍历二维数组并求和
int sum = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
}
System.out.println("年销售额:" + sum + "万元");
}
}
七、数组高级冒泡排序
package cn.hebei.sjz_数组;
/*
* 冒泡排序
* 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
*/
public class Demo4 {
public static void main(String[] args) {
int[] arr = { 2, 3, 4, 32, 14, 12, 13, 41, 31 };
// 冒泡排序
bubbleSort(arr);
// 按格式打印
printArr(arr);
}
private static void printArr(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i] + "]");
break;
}
System.out.print(arr[i] + ",");
}
}
private static void bubbleSort(int[] arr) {
// 外层循环,循环次数
for (int i = 0; i < arr.length - 1; i++) {
// 内层循环,控制比较的元素
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
package cn.hebei.sjz_数组;
/*
* 数组排序:选择排序
* 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
*/
public class Demo3 {
public static void main(String[] args) {
int[] arr = { 2, 3, 4, 32, 14, 12, 13, 41, 31 };
// 选择排序
searchSort(arr);
printArr(arr);
}
private static void printArr(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i] + "]");
break;
}
System.out.print(arr[i] + ",");
}
}
private static void searchSort(int[] arr) {
// 外层循环,给出数组元素
for (int i = 0; i < arr.length; i++) {
// 内层循环,和其他的数组元素比较
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] <= arr[j]) {
arr[i] = arr[i];
} else if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}
package cn.hebei.sjz_数组;
/*
* 二分查找:
* 前提:数组元素有序
*/
public class Demo5 {
public static void main(String[] args) {
int[] arr = { 1, 3, 4, 5, 7, 8, 12, 45, 656 };
int index = halfSearch(arr, 45);
System.out.println(index);
}
private static int halfSearch(int[] arr, int i) {
int min = 0;
int max = arr.length - 1;
int mid = (min + max) / 2;
while (arr[mid] != i) {
if (arr[mid] < i) {
min = mid + 1;
}
if (arr[mid] > i) {
max = mid - 1;
}
if (mid > max) {
return -1;
}
mid = (min + max) / 2;
}
return mid;
}
}