麻烦问一下 怎样用Java实现arraylist类 就是编写语句来代替这个类的功能

2024-12-13 20:55:01
推荐回答(2个)
回答1:

import java.util.ArrayList;

@SuppressWarnings("serial")
public class MyArrayList extends ArrayList
{

@Override
public void add(int index, Object object)
{
// TODO Auto-generated method stub
super.add(index, object);
}

@Override
public boolean add(Object object)
{
// TODO Auto-generated method stub
return super.add(object);
}

@Override
public void clear()
{
// TODO Auto-generated method stub
super.clear();
}

@Override
public Object get(int index)
{
// TODO Auto-generated method stub
return super.get(index);
}

@Override
public Object remove(int index)
{
// TODO Auto-generated method stub
return super.remove(index);
}

@Override
public boolean remove(Object object)
{
// TODO Auto-generated method stub
return super.remove(object);
}

@Override
public Object set(int index, Object object)
{
// TODO Auto-generated method stub
return super.set(index, object);
}

@Override
public int size()
{
// TODO Auto-generated method stub
return super.size();
}

}

回答2:

public static void main(String[] args) {
String a = "A", b = "B", c = "C", d = "D", e = "E";
List list = new ArrayList();
list.add(a); // 索引位置为 0
list.add(b); // 索引位置为 1
list.add(c); // 索引位置为 2
list.add(d); // 索引位置为 3
list.add(e); // 索引位置为 4