数组

数组

介绍

  • 引用数据类型

  • 保存多个同类型的数据

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

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

优缺点

  • 优点

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

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

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

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

数组的使用

声明数据

int [] iarray;

初始化数组

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])

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

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

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

遍历数组

for(int i = 0;i System.out.println(iarray[i]);
}

// 倒叙
for(int i = array.length-1;i>=0;i--){
System.out.println(iarray[i]);
}

方法参数为数组

public void printArray(int[] array){
for(int i = 0;i System.out.println(array[i]);
}
}

// 调用
int[] array = new int[4];
array = {1,2,3,4};
printArray(array);
printArray(new int[4]);
printArray(new int[]{1,2,3,4});

引用数据类型

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();
}
}

数组扩容

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

数组拷贝

System.arraycopy()

 int[] src = {1,2,3,4};
int[] dest = new int[20];
System.arraycopy(src,0,dest,0,src.length);
for(int i =0;i System.out.println(dest[i]);
}

二维数组

定义

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

初始化

int[][] arrs = {{1,2,3,4},{1},{2,3}};

二维数组的长度

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

int[][] arrays = {{1,3,4},{1}};
for(int[] i : arrays){
for(int a:i){
System.out.print(a+" ");
}
System.out.println();
}
arrays.length;

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

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

// 作业一:
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();
}
}

// 作业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;
}
}

数组工具类

  • java.util.Arrays

  • 数组排序

    • Arrays.sort()

    • 排序算法:

      • 冒泡排序

      • 选择排序

  • 查找算法

    • 直接查找

    • 二分法查找

    • 工具类

      • binarySearch 二分法查找

 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;
}
}
}
}

// 选择排序算法
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] min = j;
}
}
int temp = arrays[min];
arrays[min] = arrays[i];
arrays[i] = temp;
}

return;
}

 // 二分法查找
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] min = middle+1;
else
max = middle-1;
}
return -1;
}

你可能感兴趣的:(数组)