本文是对高淇版Java三百集,容器部分ArrayList内容学习的记录。java中ArrayList本质是一个数组,该类中包含两个属性Object[] elementData
,int size
,恰如字面意思,前一个数组用于存储数据,后者的size记录有多少个数据。
public boolean add(E e);
public void add(int index, E element);
public E remove(int index);
public boolean remove(Object o);
public E get(int index)
public E set(int index, E element)
public int indexOf(Object o)
package cn.edu.ucas.gqlearn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author: hyk
* @Date: 2020/7/19
* @version: 1.0
*/
public class MyArrayList<E> {
private Object[] elementData;
private int size;
private static final int DEFAULT_CAPACITY = 10;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
public void add(E element){
elementData[size++] = element;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i] + ",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
MyArrayList<String> myArrayList = new MyArrayList<>();
for (int i = 0; i < 9; i++) {
myArrayList.add((char)('A' + i) + "");
}
System.out.println(myArrayList.toString());
}
}
上面add方法中存在一定问题,因为固定elementData.length = 10
,会导致添加的数据过多时,出现数组越界的异常。这里给出数组扩容的代码。
package cn.edu.ucas.gqlearn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author: hyk
* @Date: 2020/7/19
* @version: 1.0
*/
public class MyArrayList<E> {
private Object[] elementData;
private int size;
private static final int DEFAULT_CAPACITY = 10;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
public void add(E element){
if (size >= elementData.length){
Object[] newArray = new Object[size + (size >> 1)];
System.arraycopy(elementData, 0, newArray, 0, size);
elementData = newArray;
}
elementData[size++] = element;
}
public E get(int index){
return (E)elementData[index];
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i] + ",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
MyArrayList<String> myArrayList = new MyArrayList<>();
for (int i = 0; i < 26; i++) {
myArrayList.add((char)('A' + i) + "");
}
System.out.println(myArrayList.toString());
System.out.println(myArrayList.get(2));
}
}
如果执行get()
方法和set()
方法,索引为负数或者索引过大,超出了size的范围,同样需要进行异常处理。代码如下:
package cn.edu.ucas.gqlearn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author: hyk
* @Date: 2020/7/19
* @version: 1.0
*/
public class MyArrayList<E> {
private Object[] elementData;
private int size;
private static final int DEFAULT_CAPACITY = 10;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int size) {
if (size < 0){
throw new RuntimeException("容器初始化错误,索引值:" + size);
}else if (size == 0){
elementData = new Object[DEFAULT_CAPACITY];
}else{
elementData = new Object[size];
}
}
public void add(E element){
if (size >= elementData.length){
Object[] newArray = new Object[size + (size >> 1)];
System.arraycopy(elementData, 0, newArray, 0, size);
elementData = newArray;
}
elementData[size++] = element;
}
public E get(int index){
checkRange(index);
return (E)elementData[index];
}
public void set(int index, E element){
checkRange(index);
elementData[index] = element;
}
public void checkRange(int index){
if (index > size){
throw new RuntimeException("容器索引值错误:" + index);
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i] + ",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
MyArrayList<String> myArrayList = new MyArrayList<>();
for (int i = 0; i < 26; i++) {
myArrayList.add((char)('A' + i) + "");
}
System.out.println(myArrayList.toString());
System.out.println(myArrayList.get(2));
//System.out.println(myArrayList.set(1, "c").toString());
myArrayList.add("宝贝欣");
myArrayList.set(1,"宝贝欣!");
System.out.println(myArrayList.toString());
}
}
package cn.edu.ucas.gqlearn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @Author: hyk
* @Date: 2020/7/19
* @version: 1.0
*/
public class MyArrayList<E> {
private Object[] elementData;
private int size;
private static final int DEFAULT_CAPACITY = 10;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int size) {
if (size < 0){
throw new RuntimeException("容器初始化错误,索引值:" + size);
}else if (size == 0){
elementData = new Object[DEFAULT_CAPACITY];
}else{
elementData = new Object[size];
}
}
public void add(E element){
if (size >= elementData.length){
Object[] newArray = new Object[size + (size >> 1)];
System.arraycopy(elementData, 0, newArray, 0, size);
elementData = newArray;
}
elementData[size++] = element;
}
public E get(int index){
checkRange(index);
return (E)elementData[index];
}
public void set(int index, E element){
checkRange(index);
elementData[index] = element;
}
public void checkRange(int index){
if (index < 0 || index >= size){
throw new RuntimeException("容器索引值错误:" + index);
}
}
public void remove(int index){
checkRange(index);
int numRemoved = elementData.length-1-index;
if (numRemoved > 0){
System.arraycopy(elementData, index+1, elementData, index, numRemoved);
}
elementData[--size] = null;
}
public void remove(E element){
for (int i = 0; i < size; i++) {
if (element.equals(get(i))){
remove(i);
}
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elementData[i] + ",");
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public boolean isEmpty(){
return size == 0 ? true:false;
}
public int size(){
return size;
}
public static void main(String[] args) {
MyArrayList<String> myArrayList = new MyArrayList<>();
for (int i = 0; i < 26; i++) {
myArrayList.add((char)('A' + i) + "");
}
System.out.println(myArrayList.toString());
System.out.println(myArrayList.get(2));
//System.out.println(myArrayList.set(1, "c").toString());
myArrayList.add("宝贝欣");
myArrayList.set(1,"宝贝欣!");
System.out.println(myArrayList.toString());
myArrayList.remove(3);
System.out.println(myArrayList.toString());
myArrayList.remove("E");
System.out.println(myArrayList.toString());
System.out.println(myArrayList.isEmpty());
System.out.println(myArrayList.size());
}
}