数组是一个相同类型的变量的集合,注意数组是长度固定的,而且本身也属于引用类型
之前说过字符串和数组经常使用,所以这里先讲一下下字符串和字符数组互转
//字符数组转成字符串
char[] arr = {'a','b','c','d','e','f'};
String b = new String(arr);
System.out.println(b);
//字符串转字符数组
char[] c = b.toCharArray();
for(int i = 0;i < c.length;i++){
System.out.println(c[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];
Arrays工具类
其中包含了已经实现了的数组各种操作,里面都是静态方法,可以直接调用
常用方法
返回值 | 函数名 | 解释 |
---|---|---|
List | asList(T... a) | 返回由指定数组支持的固定大小的集合 |
int | binarySearch(char[] a, char key) | 前提是排好序,用二分法搜索,返回下标 |
char[] | copyOf(char[] original, int newLength) | 复制指定的数组,截断或填充空字符 |
boolean | equals(char[] a, char[] a2) | 如果两个指定的字符数组彼此 |
void | fill(char[] a, char val) | 将char值分配给char数组的每个元素 |
void | sort(int[] a) | 按照数字顺序排列指定的数组 |
void | parallelSort(int[] a) | 按照数字顺序排列指定的数组,并发提高性能 |
数组实现栈
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