赞
踩
数组有一组元素(值或变量)组成的数据结构,每个元素有至少一个索引或键来标识
数组内的元素是连续存储的,所以数组中元素的地址可以通过索引计算出来,例如,
int[] array = {1, 2, 3, 4, 5};
当知道数组中0索引的地址,假设为 b b b,那么索引为1的地址为 b + 4 b+4 b+4 ,因为是整数数组,一个元素占用4个字节,其他索引的地址是 b + s i z e ∗ i b + size*i b+size∗i ,其中 s i z e size size 为整数占用字节大小4。
由此,知道数组的数据起始地址 B a s e A d d r e s s BaseAddress BaseAddress ,可由公式 B a s e A d d r e s s + s i z e ∗ i BaseAddress+size*i BaseAddress+size∗i 计算出索引 i i i 处的元素地址
byte[] array = {1, 2, 3, 4, 5};
已知array的数据的起始地址为 0 x 7138 f 94 c 8 0x7138f94c8 0x7138f94c8 ,那么索引为2元素为3的地址为多少?
因为是byte,则一个元素占用一个字节,索引为2的地址为起始地址 + 2 * 1,即, 0 x 7138 f 94 c 8 + 2 ∗ 1 = 0 x 7138 f 94 c a 0x7138f94c8 + 2 * 1 = 0x7138f94ca 0x7138f94c8+2∗1=0x7138f94ca ,因为是十六进制,索引8+2为10,即为a,
Java中的数据结构为
例如
int[] array = {1, 2, 3, 4, 5};
的大小为40个字节,组成如下
8 + 4 + 4 + 5 * 4 + 4(alignment)
根据索引查找元素,时间复杂度为 O ( 1 ) O(1) O(1)
public class DynamicArray implements Iterable<Integer>{ private int size = 0; // 逻辑大小,控制数组中的元素个数 private int capacity = 8; // 数组能存储元素的最大容量 // private int[] array = new int[capacity]; private int[] array = {}; // 初始,空数组占用空间比开始长度为capacity的数组占用空间小,在添加元素检查时,初始化容量为capacity的数组 // 尾部添加元素 public void addLast(int vlaue) { add(size, value); } // 在指定索引位置插入元素 pubic void add(int index, int value) { if (index < 0 || index > size) { System.out.println("索引错误"); } // 检查数组容量是否满,size == capacity checkAndGrow(); if (index >= 0 && index < size) { System.arraycopy(array, index, array, index + 1, size - index); } array[index] = value; size++; } // 删除指定索引的元素 public int remove(int index) { int value = array[index]; if (index < size - 1) { System.arraycopy(array, index + 1, array, index, size - index - 1); } size--; return value; } // 根据索引查找元素 public int get(int index) { return array[index]; } // 遍历数组元素,使用foreach public void foreach() { for(int i = 0; i < size; i++) { System.out.println(i); } } // 遍历方法1 使用函数式接口作为参数传递 public void foreach1(Consumer<Integer> consumer) { for(int i = 0; i < size; i++) { consumer.accept(array[i]); } } // 遍历方法2 迭代器遍历 @Override public Iterator<Integer> iterator() { return new Iterator<Integer>() { int index = 0; @Override public boolean hasNext() { // 是否有下一个元素 return index < size; } @Override public Integer next() { // 返回当前索引元素,并移动到下一个元素 return array[index++]; } }; } // 遍历方法3 使用流的方法 public IntStream stream() { return IntStream.of(Array.copyOfRange(array, 0, size)); // 拷贝左闭右开 } // 检查容量是否满 public void checkAndGrow() { if (size == 0) { return new int[capacity]; } if (size == capacity) { // 进行扩容 capacity += capacity >> 1; int[] newArray = new int[capacity]; System.arraycopy(array, 0, newArray, 0, size); array = newArray; } } }
根据索引查询元素,时间复杂度为 O ( 1 ) O(1) O(1) ;
头部插入,时间复杂度 O ( n ) O(n) O(n)
中间插入,时间复杂度 O ( n ) O(n) O(n)
尾部插入,时间复杂度 O ( 1 ) O(1) O(1)
int[][] array = {
{11, 12, 13, 14,15},
{21, 22, 23, 24,25},
{31, 32, 33, 34, 35},
};
只讨论空间局限性
只讨论空间局限性
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。