数组是一个相同类型的变量的集合,注意数组是长度固定的,而且本身也属于引用类型 之前说过字符串和数组经常使用,所以这里先讲一下下字符串和字符数组互转
// 字节/字符 数组转成字符串
char[] arr1 = {'a','b','c','d','e','f'};
byte[] arr2 = {'1','2','3'};
String a = new String(arr1);
String b = new String(arr2);
System.out.println(a);
System.out.println(b);
//字符串转 字符/字节 数组
char[] c = b.toCharArray();
for(int i = 0;i < c.length;i++){
System.out.println(c[i]);
}
byte[] d = a.getBytes();
for(int i = 0;i < d.length;i++){
System.out.println(d[i]);
}
声明创建数组
//静态声明
int[] arr = {1,2,3,4,6};
int[] arr = new int[]{1,2,3,4,5,6}
//动态声明
int[] arr = new int[10] //声明长度,不可少
arr[0] = 10 //手动赋值
二维数组
Java 并不直接支持二维数组,但也有方法解决,就是数组的元素再存储一个数组,这样就实现了二维数组了
//用这种方式创建数组,一维的大小是必须声明的
int[][] arr = new int[10][];
//不规则二维数组
int[][] arr = new int[2][]
arr[0] = new int[10];
arr[1] = new int[5];
获取长度
int[][] arr = new int[5][10];
System.out.println(arr.length);
System.out.println(arr[0].length);
数组的反转
for(int i = 0; i < arr.length/2; i++){
int temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
数组的引用传递
网图侵删
Arrays工具类
其中包含了已经实现了的数组各种操作,里面都是静态方法,可以直接调用
常用方法
返回值 | 函数名 | 解释 |
---|---|---|
List | asList(T... a) | 返回(固定大小)的集合 |
int | binarySearch(T [] a, T key) | 前提已排序,二分搜索,返回下标,否则负数 |
T [] | copyOf(T [] original, int newLength) | 复制数组,截断或填充空字符 |
T [] | copyOfRange(T [] original, int from,int to) | 复制数组,from to |
boolean | equals(T [] a, T [] a2) | 判断数组是否相等 |
void | fill(T [] a, T val) | 将char值分配给char数组的每个元素 |
void | sort(T [] a) | 给数组排序 |
String | toString(T [] a) | 返回字符串 |
这里要注意:Arrays.asList返回的是Arrays内部的集合,继承自AbstractList,没有真正实现集合功能,即不能改变其结构,若要将数组转为真集合可以使用:List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3));
数组实现栈
public class ArrayStack {
private int[] values; //存放元素
private int capacity; //容量
private int size; //元素数量
private static final int FACTOR = 2; //影响因子
//默认创造10大小
public ArrayStack() {
this.capacity = 10;
this.size = 0;
this.values = new int[capacity];
}
//指定栈大小
public ArrayStack(int initCapacity) {
if (initCapacity < 1){
throw new RuntimeException("Capacity Illegal");
}
this.capacity = initCapacity;
this.size = 0;
this.values = new int[initCapacity];
}
//扩容2倍
private void ensureCapacity(){
capacity = capacity * FACTOR;
this.values = Arrays.copyOf(values, capacity);
}
//入栈
public void push(int value){
if (size == capacity){
ensureCapacity();
}
values[size++] = value;
}
//出栈
public int pop(){
if (size-- < 0){
throw new RuntimeException("Stack is empty");
}
return values[size];
}
//返回栈顶元素
public int peek(){
if (size == 0){
throw new RuntimeException("Stack is empty");
}
return values[size-1];
}
//判断是否为空
public boolean empty(){
return size == 0;
}
//返回个数
public int size(){
return size;
}
}
测试与输出
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack();
System.out.println(arrayStack.size());
arrayStack.push(1);
arrayStack.push(2);
arrayStack.push(3);
arrayStack.push(4);
arrayStack.push(5);
arrayStack.push(6);
arrayStack.push(7);
arrayStack.push(8);
arrayStack.push(9);
arrayStack.push(10);
arrayStack.push(11);
System.out.println(arrayStack.size());
System.out.println(arrayStack.empty());
System.out.println(arrayStack.pop());
System.out.println(arrayStack.peek());
}
0
11
false
11
10
数组实现队列
public class ArrayQueue {
private int[] values; //存放元素
private int capacity; //容量大小
private int size; //元素数量
private int head; //记录头
private int tail; //记录尾
//默认构造
public ArrayQueue() {
this.capacity = 10;
this.size = 0;
this.head = 0;
this.tail = 0;
this.values = new int[capacity];
}
//指定大小
public ArrayQueue(int initCapacity) {
if (initCapacity < 1){
throw new RuntimeException("Capacity Illegal");
}
this.capacity = initCapacity;
this.size = 0;
this.head = -1;
this.tail = -1;
this.values = new int[capacity];
}
//进队
public void enQueue(int value){
if (size >= capacity){
throw new RuntimeException("Queue is full");
}
values[tail++] = value;
size++;
}
//出队
public int deQueue(){
size--;
if (size < 0){
throw new RuntimeException("Queue is empty");
}
return values[head++];
}
//是否为空
public boolean empty(){
return size == 0;
}
//返回个数
public int size(){
return size;
}
}
测试与输出
public static void main(String[] args) {
ArrayQueue arrayQueue = new ArrayQueue();
arrayQueue.enQueue(1);
arrayQueue.enQueue(2);
arrayQueue.enQueue(3);
arrayQueue.enQueue(4);
arrayQueue.enQueue(5);
arrayQueue.enQueue(6);
arrayQueue.enQueue(7);
arrayQueue.enQueue(8);
arrayQueue.enQueue(9);
arrayQueue.enQueue(10);
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println("队列大小: " + arrayQueue.size());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.deQueue());
System.out.println(arrayQueue.empty());
}
1
2
3
4
5
6
队列大小: 4
7
8
9
10
true