ObjectList Java任务

时间:2018-11-11 19:54:01

标签: java

已为我提供了以下主要方法,并且必须编写ObjectList类的代码。我应该推断出ObjectList类的必要功能并亲自编写该类,但是我不确定要实现此功能需要做什么。任何帮助理解这一点将不胜感激。这是我得到的代码:

   ObjectList ol = new ObjectList(3);

   String s = "Im Happy";
   Dog d = new Dog();
   DVD v = new DVD();
   Integer i = 1234;

   System.out.println(ol.add(s));
   System.out.println(ol.add(d));
   System.out.println(ol.add(v));
   System.out.println(ol.add(i));

   ol.remove(0);
   System.out.println(ol.add(i));


   System.out.println("Is the list full? "+ isFull()); 
   System.out.println("Is the list empty? "+ isEmpty());

   System.out.println("Total number of objects in the list: " + getTotal());

   Object g = ol.getObject(1);
   g.bark();

2 个答案:

答案 0 :(得分:0)

这很简单,只需要在Object类中使用ArrayListLinkedList创建ObjectList类型的列表,并实现以下功能

public class ObjectList{
private ArrayList<Object> objects;
public ObjectList(int size)
{
    objects = new ArrayList<Object>(size);
}

public String add (Object object)
{
    objects.add(object);
    //anything you would like to return I'm just returning a string
    return "Object Added";
}

public void remove (int index)
{
    objects.remove(index);
}

public boolean isEmpty()
{
    return objects.isEmpty();
}

public int getTotal()
{
    return objects.size();
}

public Object getObject(int index)
{
    return objects.get(index);
}
}

由于isFull()的大小可以动态更改,因此不需要ArrayList。您可以使用简单的数组代替ArrayList,然后实现isFull()函数。

另外,当使用get getObject()函数获取对象时,需要在使用there函数之前将其强制转换为正确的类型。在您的代码中g.bark()不起作用,因为Object没有树皮功能

Object g = ol.getObject(1);

//This can give a runtime error if g is not a Dog
//Use try catch when casting
Dog d = (Dog)g;

d.bark();

编辑

如果使用数组而不是isFull(),这是实现ArrayList和其他功能的方式,但是为了简单起见,请使用ArrayList版本

 public class ObjectList{
 private Object[] objects;
 private int size = 0;
 private int currentIndex = 0;


public ObjectList(int size)
{
    this.size = size;
    objects = new Object[size];
}

 private boolean isFull() {
    if(currentIndex == size)
        return true;
    else
        return false;
 }

 public String add (java.lang.Object object)
{
    if ( ! isFull() ) {
        objects[currentIndex] = object;
        currentIndex++;
        return "Object added";
    }
    return "List full : object not added";

}

public void remove (int index)
{
    if( !isEmpty() ) {
        //shifting all the object to the left of deleted object 1 index to the left to fill the empty space
        for (int i = index; i < size - 1; i++) {
            objects[i] = objects[i + 1];
        }
        currentIndex--;
    }
}

public boolean isEmpty()
{
    if(currentIndex == 0)
        return true;
    else
        return false;
}

public int getTotal()
{
    return currentIndex;
}

public java.lang.Object getObject(int index)
{
    if(index < currentIndex)
     return objects[index];
    else
        return  null;
}
}

答案 1 :(得分:0)

您似乎想要实现的是“扩展” ArrayList 的功能并创建对象的自定义列表。您可以做的是创建一个扩展ArrayList的类,并定义/覆盖您想要的任何其他方法。

public class ObjectList extends ArrayList<Object> {
//constructor with initial capacity
private int length;
public ObjectList(int size){
   super(size);
   this.length= size;
}

public Object getObject(int index){
    return this.get(index);
}
}

现在,您具有从ArrayList类和 getObject 方法继承的 add 删除函数。

关于 isFull 方法,您可以检查ObjectList类的大小是否等于

实例化的大小。
if(this.size() == this.length){
return true
}
return false;

getTotal

public int getTotal(){
return this.size();
}
相关问题