数组
数组
在Java语言中,有一种特殊的引用类型叫数组,它在概念上属于类的范畴,但处理的方式又与类和对象不完全相同。
1一维数组
数组是相同类型的数据按照顺序组合后的一种引用类型。数组可以看成是多个相同类型数据的组合,实现对这些数据的统一管理。
1、1数组的定义
数组声明的语法:
数据类型[] 数组名;
例:int[] array ;
1.2创建数组
数组的长度在创建时指定,并且一旦指定就不能更改;创建数组分为2中方式:
静态初始化:
数据类型[] 数组名 = {数组内容};
int[] array = {1,2,4,5,7,8};
动态初始化:
数据类型[] 数组名 = new 数组类型[数组长度];
char[] array = new char[10];
(ps:数组有默认值,对于数字类型,默认值为0,对于字符型,默认值为‘\u0000’,对于布尔类型,默认值为false, 对于对象类型,默认值为null)
1.3数组的属性
数组是引用类型,具有属性,常用的数组属性就是数组的长度length
(ps:数组的长度length必须>=0,length为只读,可以利用length遍历数组)
例:求数组的最大值最小值跟平均值
public static void main(String[] args) { int[] a = { 1, -12, 33}; // 先设置一个最小值,在与其它的元素进行比较,遇到比这个变量还小的元素时,就将这个元素的值 // 赋值给min这个变量 int min = a[0]; int max = a[0]; double sum = a[0]; for (int i = 0; i < a.length; i++) { if (a[i] < min) { min = a[i]; } if (a[i]>max) { max = a[i]; } sum= sum +a[i]; } System.out.println("最小值是" + min ); System.out.println("最大值是" + max ); double ret = sum/a.length; System.out.println("平均值是"+ ret); }
1.4数组的排序
冒泡排序:
它重复地走访过要排序的数组,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数组的工作是重复地进行直到没有再需要交换,也就是说该数组已经排序完成。
public static void main(String[] args) { int[] a = { 1, -12, 33 }; int temp = 0; // 冒泡排序 for (int i = 0; i < a.length - 1; i++) { for (int j = 0; j < a.length - 1 - i; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } System.out.println(Arrays.toString(a)); System.out.println("最大值是" + a[a.length - 1]); System.out.println("最小值是" + a[0]); }
1、直接定义一个变量i,给变量i复制,然后用if语句中查找数组中是否有可以相等于变量i的数。
for(int i = 0;i<arr.length-1;i++){ if (en == arr[i]) { System.out.println("存在"+en); System.out.println("位置在"+i); return; } } System.out.println("元素不存在");
2、二分查找。
二分查找的前提是需要查找的数组必须是已排序的,这里实现默认前提为升序。查找时将数组分为三部分,依次是中值(所谓的中值就是数组中间位置的那个值)前,中值,中值后;将要查找的值和数组中的值进行比较,若小于中值则在中值前面找,若大于中值则在中值后面找,等于中值时直接返回。
public class BinarySearch { public static void main(String[] args) { int[] sort = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int key = 6;// 需要查找的值 int locale = -1;// 记录 查找位置的变量 int low = 0; int high = sort.length - 1; while (high >= low) { int mid = (low + high) / 2; if (key < sort[mid]) high = mid - 1; else if (key == sort[mid]) { locale = mid; break; } else low = mid + 1; } if (locale == -1) System.out.println("数组中不存在元素" + key); else System.out.println("元素" + key + "在数组中的下标是" + locale); } }
1.6数组的复制和排序
Java中的数组的复制使用System.arraycopy方法就可以实现。
数组的复制
public static void main(String[] args) { int[] arr = {1,50,46,47,48,49}; int[] arr1 = new int[arr.length]; //目标数组的名字,被复制的起始点,要复制的数组名,复制的起始点,要复制的长度 System.arraycopy(arr, 0, arr1, 0, arr.length); System.out.println(Arrays.toString(arr1)); }
数组的排序
Arrays.sort(数组名)
Arrays.sort(arr_name)
Arrays.sort(arr_name,fromIndex,toIndex)
(数组名称,排序的起始下标,排序的终止下表)
例:
class ArraySort_sample { public static void main(String args[]){ int[ ] point = {1,6,2,3,9,4,5,7,8}; Arrays.sort(point); for(int i=0;i<point.length;i++){ System.out.print(point[i]+" "); } } }
将一个数组中的元素逆序排放
public class Demo01 { public static void main(String[] args) { //将数组中的团素逆序排放; int[] arr = {1,2,3,4,5,6,7}; int len = arr.length; for(int i = 1;i<= len/2;i++){ int temp = arr[i-1]; arr[i-1] = arr[len-i]; arr[len-i]=temp; } System.out.println(Arrays.toString(arr)); } }
将一个数组中的重复元素保留一个其他的清零。
public static void main(String[] args) { // 创建一个数组; int[] arr = { 1, 2, 1, 3, 2, 3, 1, 2, 3 }; int l = arr.length; for (int i = 0; i < l - 1; i++) { // 优化循环的效率,让我们去除待比较的元素为0时,后续的循环就没必要再执行 if (arr[0] == 0) { continue; } // 用元素i后面的元素依次和元素i进行对比; for (int j = i + 1; j < l; j++) { // 如果元素i和元素J相等,将0复制给 if (arr[j] == arr[i]) { arr[j] = 0; } } } System.out.println(Arrays.toString(arr)); }
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术