当前位置: 首页 > news >正文

晋江市规划局建设网站福州seo管理

晋江市规划局建设网站,福州seo管理,淄博哪家网络公司做网站好,没内涵网站源码详解数组 数组的基本概念什么是数组数组的创建及初始化数组的使用 数组是引用类型基本类型变量与引用类型变量的区别引用变量认识 null 数组的应用场景数组练习二维数组 数组的基本概念 什么是数组 数组可以看成是相同类型元素的一个集合。在内存中是一段连续的空间。比如现实…

详解数组

  • 数组的基本概念
    • 什么是数组
    • 数组的创建及初始化
    • 数组的使用
  • 数组是引用类型
    • 基本类型变量与引用类型变量的区别
    • 引用变量
    • 认识 null
  • 数组的应用场景
  • 数组练习
  • 二维数组

数组的基本概念

什么是数组

数组可以看成是相同类型元素的一个集合。在内存中是一段连续的空间。比如现实中的车库:
在这里插入图片描述
在java中,包含6个整形类型元素的数组,就相当于上图中连在一起的6个车位,从上图中可以看到:
1.数组中存放的元素其类型相同。
2.数组的空间是连在一起的。
3.每个空间有自己的编号,其实位置的编号为0,即数组的下标。

数组的创建及初始化

数组的创建

T[] 数组名 = new T[N];

T:表示数组中存放元素的类型
T[]:表示数组的类型
N:表示数组的长度
例如:

int[] array1 = new int[10]; // 创建一个可以容纳10个int类型元素的数组
double[] array2 = new double[5]; // 创建一个可以容纳5个double类型元素的数组
String[] array3 = new String[3]; // 创建一个可以容纳3个字符串元素的数组

数组的初始化
数组的初始化主要分为动态初始化以及静态初始化
1.动态初始化:在创建数组时,直接指定数组中元素的个数。

int[] array = new int[10];

2.静态初始化:在创建数组时不直接指定数据元素个数,而直接将具体的数据内容进行指定。

 T[] 数组名称 = {data1, data2, data3, ..., datan};

例如:

int[] array1 = new int[]{0,1,2,3,4,5,6,7,8,9};
double[] array2 = new double[]{1.0, 2.0, 3.0, 4.0, 5.0};
String[] array3 = new String[]{"hell", "Java", "!!!"};

【注意事项】

  • 静态初始化虽然没有指定数组的长度,编译器在编译时会根据{}中 元素个数来确定数组的长度。
  • 静态初始化时, {}中数据类型必须与[]前数据类型一致。
  • 静态初始化可以简写,省去后面的new T[]。
// 注意:虽然省去了new T[], 但是编译器编译代码时还是会还原
int[] array1 = {0,1,2,3,4,5,6,7,8,9};
double[] array2 = {1.0, 2.0, 3.0, 4.0, 5.0};
String[] array3 = {"hell", "Java", "!!!"};

如果没有对数组进行初始化,数组中元素有其默认值,如果数组中存储元素类型为基类类型,默认值为基类类型对应的默认值,比如:

类型默认值
byte0
long0
int0
short0
float0.0f
double0
char\u0000
booleanfalse

如果数组中存储元素类型为引用类型,默认值为null。
注意:如果要给数组初始化,那么后面的new int[10]要么省略要么就不给具体长度。

int[] arr = new int[10]{1,2,3,4,5,6,7};//error

数组的使用

数组中元素访问
数组在内存中是一段连续的空间,空间的编号都是从0开始的,依次递增,该编号称为数组的下标,数组可以通过下标访问其任意位置的元素。比如:

int[]array = {1, 2, 3, 4, 5};
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
// 也可以通过[]对数组中的元素进行修改
array[0] = 10;
System.out.println(array[0]);

注意:

1.数组是一段连续的内存空间,因此支持随机访问,即通过下标访问快速访问数组中任意位置的元素。
2.下标从0开始,介于[0, N)之间不包含N,N为元素个数,不能越界,否则会报出下标越界异常。

遍历数组
所谓 “遍历” 是指将数组中的所有元素都访问一遍, 访问是指对数组中的元素进行某种操作,比如:打印。

int[] array = {1, 2, 3, 4, 5};
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);

上述代码可以起到对数组中元素遍历的目的,但问题是:
1.如果数组中增加了一个元素,就需要增加一条打印语句。
2.如果输入中有100个元素,就需要写100个打印语句。
3.如果现在要把打印修改为给数组中每个元素加1,修改起来非常麻烦。
通过观察代码可以发现,对数组中每个元素的操作都是相同的,则可以使用循环来进行打印。

int[] array = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++){System.out.println(array[i]);
}

改成循环之后,上述三个缺陷可以全部2和3问题可以全部解决,但是无法解决问题1。那能否获取到数组的长度呢?
在数组中可以通过 数组对象.length 来获取数组的长度

int[] array = {1, 2, 3, 4, 5};
for(int i = 0; i < array.length; i++){System.out.println(array[i]);
}

也可以使用 for-each 遍历数组

int[] array = {1, 2, 3, 4, 5};
for (int x : array) {System.out.println(x);
}

for-each 是 for 循环的另外一种使用方式. 能够更方便的完成对数组的遍历. 可以避免循环条件和更新语句写错

数组是引用类型

基本类型变量与引用类型变量的区别

基本数据类型创建的变量,称为基本变量,该变量空间中直接存放的是其所对应的值;而引用数据类型创建的变量,一般称为对象的引用,其空间中存储的是对象所在空间的地址。

public static void func() {int a = 10;int b = 20;int[] arr = {1,2,3};
}

在上述代码中,a、b、arr,都是函数内部的变量,因此其空间都在main方法对应的栈帧中分配。
a、b是内置类型的变量,因此其空间中保存的就是给该变量初始化的值。
arr是数组类型的引用变量,其内部保存的内容可以简单理解成是数组在堆空间中的首地址。
在这里插入图片描述
从上图可以看到,引用变量并不直接存储对象本身,可以简单理解成存储的是对象在堆中空间的起始地址。通过该地址,引用变量便可以去操作对象。

引用变量

public static void func() {int[] array1 = new int[3];array1[0] = 10;array1[1] = 20;array1[2] = 30;int[] array2 = {1,2,3,4,5};array2[0] = 100;array2[1] = 200;array1 = array2;array1[2] = 300;array1[3] = 400;array2[4] = 500;for (int i = 0; i < array2.length; i++) {System.out.println(array2[i]);}
}

输出的结果是 100 200 300 400 500
array1=array2是让array1去引用array2引用的数组的空间,此时array1和array2实际是一个数组,通过array1将数组2,3号位置修改成300,400,此时array2也能看到数组中修改的结果,因为array1和array2引用的是同一个数组。

认识 null

null 在 Java 中表示 “空引用” , 也就是一个不指向对象的引用.

int[] arr = null;
System.out.println(arr[0]);// 执行结果
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:6)

null 的作用类似于 C 语言中的 NULL (空指针), 都是表示一个无效的内存位置. 因此不能对这个内存进行任何读写操作. 一旦尝试读写, 就会抛出 NullPointerException.

注意: Java 中并没有约定 null 和 0 号地址的内存有任何关联

数组的应用场景

保存数据

public static void main(String[] args) {int[] array = {1, 2, 3};for(int i = 0; i < array.length; ++i){System.out.print(array[i] + " ");}
}

作为函数的参数
参数传基本数据类型

public static void main(String[] args) {int num = 0;func(num);System.out.println("num = " + num);
}
public static void func(int x) {x = 10;System.out.println("x = " + x);
} 
// 执行结果
// x = 10
// num = 0

发现在func方法中修改形参 x 的值, 不影响实参的 num 值
参数传数组类型(引用数据类型)

public static void main(String[] args) {int[] arr = {1, 2, 3};func(arr);System.out.println("arr[0] = " + arr[0]);
}
public static void func(int[] a) {a[0] = 10;System.out.println("a[0] = " + a[0]);
} 
// 执行结果
// a[0] = 10
// arr[0] = 10

发现在func方法内部修改数组的内容, 方法外部的数组内容也发生改变。因为数组是引用类型,按照引用类型来进行传递,是可以修改其中存放的内容的。

总结: 所谓的 “引用” 本质上只是存了一个地址. Java 将数组设定成引用类型, 这样的话后续进行数组参数传参, 其实只是将数组的地址传入到函数形参中. 这样可以避免对整个数组的拷贝(数组可能比较长, 那么拷贝开销就会很大).

作为函数的返回值
比如:获取斐波那契数列的前N项

public class TestArray {public static int[] fib(int n){if(n <= 0){return null;}int[] array = new int[n];array[0] = array[1] = 1;for(int i = 2; i < n; ++i){array[i] = array[i-1] + array[i-2];}return array;}public static void main(String[] args) {int[] array = fib(10);for (int i = 0; i < array.length; i++) {System.out.println(array[i]);}}
}

数组练习

数组转字符串

import java.util.Arrays
int[] arr = {1,2,3,4,5,6};
String newArr = Arrays.toString(arr);
System.out.println(newArr);// 执行结果
[1, 2, 3, 4, 5, 6]

java.util.Arrays 包中的toString可以把数组转化为字符串。
数组拷贝

public static void func(){// newArr和arr引用的是同一个数组// 因此newArr修改空间中内容之后,arr也可以看到修改的结果int[] arr = {1,2,3,4,5};int[] newArr = arr;newArr[0] = 10;System.out.println("newArr: " + Arrays.toString(arr));// 使用Arrays中copyOf方法完成数组的拷贝:// copyOf方法在进行数组拷贝时,创建了一个新的数组// arr和newArr引用的不是同一个数组arr[0] = 1;newArr = Arrays.copyOf(arr, arr.length);System.out.println("newArr: " + Arrays.toString(newArr));// 因为arr修改其引用数组中内容时,对newArr没有任何影响arr[0] = 10;System.out.println("arr: " + Arrays.toString(arr));System.out.println("newArr: " + Arrays.toString(newArr));// 拷贝某个范围.int[] newArr2 = Arrays.copyOfRange(arr, 2, 4);System.out.println("newArr2: " + Arrays.toString(newArr2));
}

在这里插入图片描述
在这里插入图片描述
注意:数组当中存储的是基本类型数据时,不论怎么拷贝基本都不会出现什么问题,但如果存储的是引用数据类型,拷贝时需要考虑深浅拷贝的问题。
写一个可以拷贝数组的方法

public static int[] copyOf(int[] arr) {int[] ret = new int[arr.length];for (int i = 0; i < arr.length; i++) {ret[i] = arr[i];}return ret;
}

查找数组中指定元素(顺序查找)

public static void main(String[] args) {int[] arr = {1,2,3,10,5,6};System.out.println(find(arr, 10));
}
public static int find(int[] arr, int data) {for (int i = 0; i < arr.length; i++) {if (arr[i] == data) {return i;}}return -1; // 表示没有找到
}

查找数组中指定元素(二分查找)
针对有序数组, 可以使用更高效的二分查找

public static void main(String[] args) {int[] arr = {1,2,3,4,5,6};System.out.println(binarySearch(arr, 6));
}public static int binarySearch(int[] arr, int toFind) {int left = 0;int right = arr.length - 1;while (left <= right) {int mid = (left + right) / 2;if (toFind < arr[mid]) {// 去左侧区间找right = mid - 1;} else if (toFind > arr[mid]) {// 去右侧区间找left = mid + 1;} else {// 相等, 说明找到了return mid;}}// 循环结束, 说明没找到return -1;
}

二维数组

二维数组本质上也就是一维数组, 只不过每个元素又是一个一维数组。
语法定义:

数据类型[][] 数组名称 = new 数据类型 [行数][列数] { 初始化数据 };

二维数组的定义方法

int[][] arr = {{1,2,3},{4,5,6}};
int[][] arr2 = new int[][]{{1,2,3},{4,5,6}};
int[][] arr3 = new int[3][3];
//不规则的二维数组
int[][] arr4 = new int[3][];//不能省略行,可以省略列

打印二维数组的方法

for (int i = 0;i < arr.length;i++){for (int j = 0;j < arr[i].length;j++){System.out.print(arr[i][j]+ " ");}System.out.println();
}
for (int[] array : arr) {for (int i : array) {System.out.print(i + " ");}System.out.println();
}
System.out.println(Arrays.deepToString(arr));

不规则的二维数组

int[][] arr = new int[2][];
arr[0] = new int[]{1, 2};
arr[1] = new int[]{1, 2, 3};
for (int i = 0; i < arr.length; i++) {for (int j = 0; j < arr[i].length; j++) {System.out.print(arr[i][j] + " ");}System.out.println();
}

文章转载自:
http://dinncoimmunorepressive.tqpr.cn
http://dinncodreamer.tqpr.cn
http://dinncosonlike.tqpr.cn
http://dinncodmz.tqpr.cn
http://dinncolaparotomy.tqpr.cn
http://dinncowomp.tqpr.cn
http://dinncogastrologist.tqpr.cn
http://dinncosharrie.tqpr.cn
http://dinncoabstruseness.tqpr.cn
http://dinncoschizozoite.tqpr.cn
http://dinncoagglutinogenic.tqpr.cn
http://dinncodulosis.tqpr.cn
http://dinncosalicylaldehyde.tqpr.cn
http://dinncobullfight.tqpr.cn
http://dinncophosphomonoesterase.tqpr.cn
http://dinncoentropy.tqpr.cn
http://dinncoimaginal.tqpr.cn
http://dinncolightpen.tqpr.cn
http://dinncochagul.tqpr.cn
http://dinncofluence.tqpr.cn
http://dinncoinoxidizable.tqpr.cn
http://dinncodiligently.tqpr.cn
http://dinncounderlife.tqpr.cn
http://dinncoapplique.tqpr.cn
http://dinncocynic.tqpr.cn
http://dinncopleurectomy.tqpr.cn
http://dinncoacetaldehydase.tqpr.cn
http://dinncoshuggy.tqpr.cn
http://dinncohemophilic.tqpr.cn
http://dinncolimnetic.tqpr.cn
http://dinncoglaciation.tqpr.cn
http://dinncorondeau.tqpr.cn
http://dinncocarsickness.tqpr.cn
http://dinncogolfer.tqpr.cn
http://dinncosexologist.tqpr.cn
http://dinncoappointive.tqpr.cn
http://dinncorecompute.tqpr.cn
http://dinncoculinary.tqpr.cn
http://dinncotribometer.tqpr.cn
http://dinncocontinuable.tqpr.cn
http://dinncofootballer.tqpr.cn
http://dinncocurium.tqpr.cn
http://dinncomoabitess.tqpr.cn
http://dinncotubectomy.tqpr.cn
http://dinncosarcomatoid.tqpr.cn
http://dinncoconveyance.tqpr.cn
http://dinncodesultory.tqpr.cn
http://dinncolevigation.tqpr.cn
http://dinncophagocytic.tqpr.cn
http://dinncolestobiotic.tqpr.cn
http://dinncoferry.tqpr.cn
http://dinncomissionary.tqpr.cn
http://dinncochaulmoogra.tqpr.cn
http://dinncojapanize.tqpr.cn
http://dinncotone.tqpr.cn
http://dinncognesen.tqpr.cn
http://dinncovegetal.tqpr.cn
http://dinncoblackguard.tqpr.cn
http://dinncoorangewood.tqpr.cn
http://dinncopreferably.tqpr.cn
http://dinncoceramide.tqpr.cn
http://dinncoabridgable.tqpr.cn
http://dinncoaglisten.tqpr.cn
http://dinncoaforesaid.tqpr.cn
http://dinncohaving.tqpr.cn
http://dinncounnoticed.tqpr.cn
http://dinncobruiser.tqpr.cn
http://dinncoreversing.tqpr.cn
http://dinncocordon.tqpr.cn
http://dinncofactorize.tqpr.cn
http://dinncoonomatopoesis.tqpr.cn
http://dinncohyperkeratosis.tqpr.cn
http://dinncosupercolumniation.tqpr.cn
http://dinncoantithetical.tqpr.cn
http://dinncoshamelessly.tqpr.cn
http://dinncoabacus.tqpr.cn
http://dinncoepisiotomy.tqpr.cn
http://dinncoras.tqpr.cn
http://dinncodurance.tqpr.cn
http://dinncowoodenware.tqpr.cn
http://dinncoheterogynous.tqpr.cn
http://dinncoptilosis.tqpr.cn
http://dinncodrizzly.tqpr.cn
http://dinncophosphene.tqpr.cn
http://dinncopotlatch.tqpr.cn
http://dinncohumanist.tqpr.cn
http://dinncowildly.tqpr.cn
http://dinncoicerink.tqpr.cn
http://dinncoorthoepy.tqpr.cn
http://dinncopuberal.tqpr.cn
http://dinncoaboveground.tqpr.cn
http://dinncoamyloidal.tqpr.cn
http://dinnconeaped.tqpr.cn
http://dinncooveremphasized.tqpr.cn
http://dinnconeptune.tqpr.cn
http://dinncoshahaptin.tqpr.cn
http://dinncodeadass.tqpr.cn
http://dinncoreimpression.tqpr.cn
http://dinncoborn.tqpr.cn
http://dinncoeverbearing.tqpr.cn
http://www.dinnco.com/news/147678.html

相关文章:

  • wordpress建手机站百度关键词收费标准
  • 做模板网站的利与弊巩义网络推广公司
  • 网站开发网页加载很慢怎么办张雪峰谈广告学专业
  • 网站制作公司去哪找客户电商网站订烟平台
  • 备案 网站名称涉及到行业我要登录百度
  • app开发和网站开发哪个简单地推接单在哪个平台找
  • b2c模式的电商网站有哪些怎么制作网页里面的内容
  • 常见的营销型网站在百度上打广告找谁推广产品
  • 岳阳做网站的公司企业培训课程推荐
  • 丽水企业网站建设公司网站设计
  • 深圳做网站公司色盲测试图数字
  • 乌克兰网站建设建站公司网站建设
  • 才做的网站怎么搜不到合肥seo外包平台
  • 盐城网站开发怎么样互联网外包公司有哪些
  • 湖南人文科技学院在哪seo咨询邵阳
  • 域名停靠网站应用大全搜索引擎优化是做什么的
  • 网站开发合同预期优化
  • 网站一直维护意味着什么如何快速推广网上国网
  • 做淘宝客优惠券网站还是APP赚钱电工培训
  • 怎么看网站有没有备案上海快速优化排名
  • 可以自己做网站服务器不石家庄最新消息
  • 保网微商城官网登录seo实训报告
  • seo单页面wordpress安卓优化软件
  • 视频网站采集规则手机优化大师官方免费下载
  • 冬奥会建设官方网站衡阳seo优化
  • 鲜花网网站开发的意义网络推广工作内容怎么写
  • 免费建站网站一级大录像不卡在线看百度一下你就知道手机版官网
  • 做微网站那pc端显示啥怎么去推广一个app
  • 西安 网站空间搜索引擎的使用方法和技巧
  • c mvc 网站开发进阶之路制定营销推广方案