MyArrayList myArrayList = new MyArrayList(); myArrayList.add(1); myArrayList.add(2); myArrayList.add(3); myArrayList.display(); /*myArrayList.add(5); myArrayList.display();*/ try{ myArrayList.add(1,99); }catch (AddIndexOutOfException e) { e.printStackTrace(); } myArrayList.display(); }怎么给他横着打印
时间: 2024-03-04 08:49:07 浏览: 22
您可以将 `MyArrayList` 类中的 `display()` 方法进行修改,改为横向打印元素。以下是一种可能的实现方式:
```java
public void display() {
if (size == 0) {
System.out.println("[]");
return;
}
System.out.print("[");
for (int i = 0; i < size - 1; i++) {
System.out.print(elementData[i] + ", ");
}
System.out.println(elementData[size - 1] + "]");
}
```
这个实现方式在打印元素时,首先输出一个左中括号 `[`,然后依次输出每个元素和一个逗号 `,`,最后输出一个右中括号 `]`。这样就能够横向打印元素了。
相关问题
参照ArrayList类的功能,创建自己的ArrayList类:MyArrayList.
好的,让我们开始创建 MyArrayList 类。
```java
public class MyArrayList<E> {
private static final int DEFAULT_CAPACITY = 10;
private Object[] elementData;
private int size = 0;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "
+ initialCapacity);
elementData = new Object[initialCapacity];
}
public void add(E e) {
if (size == elementData.length) {
ensureCapacity();
}
elementData[size++] = e;
}
@SuppressWarnings("unchecked")
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
return (E) elementData[index];
}
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
E removedElement = get(index);
for (int i = index; i < size - 1; i++) {
elementData[i] = elementData[i + 1];
}
elementData[--size] = null;
return removedElement;
}
public int size() {
return size;
}
private void ensureCapacity() {
int newCapacity = elementData.length * 2;
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
```
这里我们使用了泛型,这样就可以让 MyArrayList 类型安全地存储任何类型的对象。我们还定义了两个构造函数,一个是使用默认容量创建 MyArrayList 对象,另一个是可以指定容量的构造函数。
add() 方法用于将元素添加到 MyArrayList 中。如果 MyArrayList 已满,我们就需要扩容,这里我们使用了一个 ensureCapacity() 方法来实现。get() 方法用于获取指定索引位置上的元素,remove() 方法用于删除指定索引位置上的元素,size() 方法用于获取 MyArrayList 中元素的数量。
这就是我们的 MyArrayList 类的实现。
定义一个泛型类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
```