数组

作者: Hao_38b9 | 来源:发表于2020-06-28 19:53 被阅读0次

数组

介绍

  • 引用数据类型

  • 保存多个同类型的数据

  • 可存储基本数据类型数据,也可以存储引用数据类型的数据

  • 存储的数据内存地址连续

优缺点

  • 优点

    • 查找某个元素的效率极高

      • 下标的计算不复杂:首地址+下标x类型在内存中所占大小
  • 缺点

    • 删除或者修改的时候,为了保证内存地址连续,需要将后面所有的数据向前或者向后迁移,效率降低

    • 不能存储大数据量,因为需要保证一段连续的内存地址

数组的使用

声明数据

int [] iarray;

初始化数组

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n60" mdtype="fences">int [] iarray = new int[5]; // 什么5个元素的数组,初始值为0
String[] sarray = new String[5];
int[] iarray = {1,2,3,4} // 静态初始化
System.out.println(iarray[0]);
System.out.println(iarray[iarray.length-1])</pre>

  • 每个数组都有length属性,标志数组的长度

  • 静态初始化 int[] array = {....}

  • 动态初始化 int[] array = new int[5]

遍历数组

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n69" mdtype="fences">for(int i = 0;i<array.length;i++){
System.out.println(iarray[i]);
}</pre>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n70" mdtype="fences">// 倒叙
for(int i = array.length-1;i>=0;i--){
System.out.println(iarray[i]);
}</pre>

方法参数为数组

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n82" mdtype="fences">public void printArray(int[] array){
for(int i = 0;i<array.length;i++){
System.out.println(array[i]);
}
}</pre>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n84" mdtype="fences">// 调用
int[] array = new int[4];
array = {1,2,3,4};
printArray(array);
printArray(new int[4]);
printArray(new int[]{1,2,3,4});</pre>

引用数据类型

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n90" mdtype="fences">class Animal{
public void move(){
System.out.println("动物");
}
}
class Bird extends Animal{
public void move(){
System.out.println("鸟");
}
}
public class Test{
public static void main(String[] args){
Animal[] animals = new Animal[4];
for(int i = 0;i<4;i++){
animals[i] = new Bird();
}
animals[1].move();
}
}</pre>

数组扩容

  • 数组满了,需要新建一个大的数据,进行所有数据的迁移,这种行为会涉及到数据的拷贝,使运行效率降低

数组拷贝

System.arraycopy()

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n104" mdtype="fences"> int[] src = {1,2,3,4};
int[] dest = new int[20];
System.arraycopy(src,0,dest,0,src.length);
for(int i =0;i<dest.length;i++){
System.out.println(dest[i]);
}</pre>

二维数组

定义

二维数组的元素是一维数组

初始化

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n114" mdtype="fences">int[][] arrs = {{1,2,3,4},{1},{2,3}};</pre>

二维数组的长度

Tips: java 数组不是矩阵类型的,二维数组的每个一维数组元素的长度可以不相同。不会自动填充

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n122" mdtype="fences">int[][] arrays = {{1,3,4},{1}};
for(int[] i : arrays){
for(int a:i){
System.out.print(a+" ");
}
System.out.println();
}
arrays.length;</pre>

[图片上传失败...(image-4fbc43-1593345186441)]

[图片上传失败...(image-a97007-1593345186441)]

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n129" mdtype="fences">// 作业一:
public class MyStack {
Object[] objects;
int index;

public MyStack(int length) {
this.objects = new Object[length];
this.index = 0;
}
public void push(Object object){
if(index==objects.length){
System.out.println("栈满了");
return;
}
objects[index] = object;
index++;
}
public Object pop(){
index--;
if(index<0){
System.out.println("栈空了");
return null;
}
return objects[index];
}

public static void main(String[] args) {
MyStack myStack = new MyStack(2);
myStack.push(new Object());
myStack.push(new Object());
myStack.push(new Object());
myStack.pop();
myStack.pop();
myStack.pop();
}
}
​</pre>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n130" mdtype="fences">// 作业2:
import java.util.Scanner;

public class HotelManage {
private Room[][] rooms;

public HotelManage() {
this.rooms = new Room[][]{{new Room(1), new Room(2)},{new Room(3),new Room(4)}};
}
public void bookRoom(int id){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
if (rooms[i][j].id==id){
rooms[i][j].isFree=false;
System.out.println("预定成功");
return;
}
}
}
System.out.println("该房间已经被预定了,请重新选择");
return;
}
public void cancelRoom(int id){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
if (rooms[i][j].id==id){
rooms[i][j].isFree=true;
System.out.println("取消成功");
return;
}
}
}
System.out.println("该房间没有被预定了,请重新选择");
return;
}
public void roomState(){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
System.out.println(rooms[i][j].toString());
}
}
}

public static void main(String[] args) {
HotelManage hotelManage =new HotelManage();
hotelManage.roomState();
int id;
Scanner scanner = new Scanner(System.in);
id = scanner.nextInt();
System.out.println(id);
hotelManage.bookRoom(id);
hotelManage.cancelRoom(id);
}
}
class Room{
int id;
boolean isFree;
RoomType roomType;
public Room(){
this.id = 0;
this.isFree = true;
this.roomType = RoomType.SMALL;
}

public Room(int id) {
this.id = id;
this.isFree = true;
this.roomType = RoomType.SMALL;
}

public Room(int id, boolean isFree, RoomType roomType) {
this.id = id;
this.isFree = isFree;
this.roomType = roomType;
}

@Override
public String toString() {
return "Room{" +
"id=" + id +
", isFree=" + isFree +
", roomType=" + roomType +
'}';
}
}
enum RoomType{
BIG("大房间"),SMALL("小房间"),MID("中等房间");
String name;

RoomType(String name) {
this.name = name;
}

@Override
public String toString() {
return name;
}
}
​</pre>

数组工具类

  • java.util.Arrays

  • 数组排序

    • Arrays.sort()

    • 排序算法:

      • 冒泡排序

      • 选择排序

  • 查找算法

    • 直接查找

    • 二分法查找

    • 工具类

      • binarySearch 二分法查找

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n155" mdtype="fences"> public static void bubbleSort(int[] arrays){
for (int i = 0; i < arrays.length - 1; i++) {
for (int j = 0; j < arrays.length - i - 1; j++) {
if(arrays[j]>arrays[j+1]){
int temp = arrays[j];
arrays[j] = arrays[j+1];
arrays[j+1] = temp;
}
}
}
}</pre>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n183" mdtype="fences">// 选择排序算法
public static void selectSort(int[] arrays){
for (int i = 0; i < arrays.length-1; i++) {
int min=i+1;
for (int j = i+1; j < arrays.length; j++) {
if (arrays[j]<arrays[min]){
min = j;
}
}
int temp = arrays[min];
arrays[min] = arrays[i];
arrays[i] = temp;
}

return;
}</pre>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" spellcheck="false" cid="n188" mdtype="fences"> // 二分法查找
public static int binaryFind(int[] arrays,int dest){
int min = 0;
int max = arrays.length;
while (min<=max){
int middle = (max+min)/2;
if(arrays[middle]==dest)
return middle;
else if(arrays[middle]<dest)
min = middle+1;
else
max = middle-1;
}
return -1;
}</pre>

相关文章

  • 数组

    数组数组数组数组数组数组数组数组数组

  • JavaScript - 5.数组<增删改查>

    数组 Array 数组 - 增 数组 - 删 / 改 数组 - 查 数组 - 自动 toString() 数组 -...

  • PHP数组使用

    数组定义 数组增、删、改 数组查询 数组排序 数组合并、分割 数组比较、去重复 数组长度 数组遍历 数组转换 其他...

  • 》》》PHP初入---(三)

    数组定义 1.索引数组:数组下标是整型的 声明数组: 访问数组: count(数组)--获取数组长度 查看数组所有...

  • JavaScript中数组的常用操作

    数组的遍历 数组的映射 数组的简化 数组的连接 获取数组的片段 数组的拷贝 查找数组 数组去重

  • JavaSE之数组

    六、数组 目录:数组概述、数组声明创建、数组使用、多维数组、Array类、稀疏数组 1.什么是数组 数组的定义:数...

  • Shell数组、关联数组

    数组 定义数组 获取数组 关联数组 定义关联数组 获取关联数组

  • 学习Java第五天

    数组是多个数据的集合 数组的语法 数组元素类型【】 数组名; 多维数组: 数组元素类型【】【】 数组名; 多维数组...

  • php基础精粹

    PHP php数组 php数组之索引数组初始化 PHP数组之索引数组赋值 PHP数组之访问索引数组内容 PHP数组...

  • C语言的惯用集

    数组部分 数组部分 清空数组a 把数据读进数组a 对数组a求和

网友评论

      本文标题:数组

      本文链接:https://www.haomeiwen.com/subject/ctfgfktx.html