定义一个泛型类MyArrayList,实现数组列表的基本操作:add、remove、get、size。
时间: 2024-06-03 07:10:01 浏览: 12
好的,这是一个关于Java编程的问题,我可以回答。MyArrayList是一个泛型类,可以存储任何类型的数据。实现add方法,可以向列表中添加元素;实现remove方法,可以从列表中删除元素;实现get方法,可以获取指定位置的元素;实现size方法,可以获取列表中的元素数量。具体实现可以参考Java集合框架中ArrayList类的实现方式。
相关问题
定义一个泛型类MyArrayList,实现数组列表的基本操作: add、remove、get、size
```java
public class MyArrayList<T> {
private Object[] array;
private int size;
public MyArrayList() {
this.array = new Object[10];
this.size = 0;
}
public void add(T element) {
if (size == array.length) {
Object[] newArray = new Object[array.length * 2];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
array[size++] = element;
}
public void remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
System.arraycopy(array, index + 1, array, index, size - index - 1);
array[--size] = null;
}
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return (T) array[index];
}
public int size() {
return size;
}
}
```
使用示例:
```java
MyArrayList<String> list = new MyArrayList<>();
list.add("foo");
list.add("bar");
list.add("baz");
System.out.println(list.get(1)); // 输出 "bar"
list.remove(0);
System.out.println(list.size()); // 输出 2
```
定义一个泛型类MyStack,实现栈的基本操作:push、pop、peek、isEmpty。 定义一个泛型类MyQueue,实现队列的基本操作:add、remove、peek、isEmpty。 定义一个泛型类MyArrayList,实现数组列表的基本操作:add、remove、get、size。
好的,我可以为您解答这个问题。
以下是定义泛型类MyStack的代码:
```java
public class MyStack<T> {
private ArrayList<T> list;
public MyStack() {
list = new ArrayList<>();
}
public void push(T item) {
list.add(item);
}
public T pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
return list.remove(list.size() - 1);
}
public T peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return list.get(list.size() - 1);
}
public boolean isEmpty() {
return list.isEmpty();
}
}
```
以下是定义泛型类MyQueue的代码:
```java
public class MyQueue<T> {
private LinkedList<T> list;
public MyQueue() {
list = new LinkedList<>();
}
public void add(T item) {
list.addLast(item);
}
public T remove() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return list.removeFirst();
}
public T peek() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return list.getFirst();
}
public boolean isEmpty() {
return list.isEmpty();
}
}
```
以下是定义泛型类MyArrayList的代码:
```java
public class MyArrayList<T> {
private T[] array;
private int size;
public MyArrayList() {
array = (T[]) new Object[10];
size = 0;
}
public void add(T item) {
if (size == array.length) {
resize();
}
array[size++] = item;
}
public T remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
T item = array[index];
for (int i = index; i < size - 1; i++) {
array[i] = array[i + 1];
}
array[--size] = null;
return item;
}
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return array[index];
}
public int size() {
return size;
}
private void resize() {
T[] newArray = (T[]) new Object[array.length * 2];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
}
```
以上是定义泛型类MyStack、MyQueue、MyArrayList的代码,它们分别实现了栈、队列、数组列表的基本操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)