对象列表 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();


翻阅古今
浏览 168回答 2
2回答

jeck猫

这是很简单只需要创建的列表,Object使用类型ArrayList或LinkedList在ObjectList类并实现功能如下public class ObjectList{private ArrayList<Object> objects;public ObjectList(int size){&nbsp; &nbsp; objects = new ArrayList<Object>(size);}public String add (Object object){&nbsp; &nbsp; objects.add(object);&nbsp; &nbsp; //anything you would like to return I'm just returning a string&nbsp; &nbsp; return "Object Added";}public void remove (int index){&nbsp; &nbsp; objects.remove(index);}public boolean isEmpty(){&nbsp; &nbsp; return objects.isEmpty();}public int getTotal(){&nbsp; &nbsp; return objects.size();}public Object getObject(int index){&nbsp; &nbsp; return objects.get(index);}}在isFull()没有必要的,因为ArrayList大小可动态变化。您可以使用简单的数组代替ArrayList然后实现该isFull()函数。此外,当使用 getgetObject()函数获取对象时,您需要在使用该函数之前将其转换为正确的类型。在您的代码中g.bark()不起作用,因为Object没有树皮功能Object g = ol.getObject(1);//This can give a runtime error if g is not a Dog//Use try catch when castingDog d = (Dog)g;d.bark();编辑isFull()如果使用数组而不是ArrayList为了简单起见,请使用该ArrayList版本,这就是您将如何实现和其他功能的方式&nbsp;public class ObjectList{&nbsp;private Object[] objects;&nbsp;private int size = 0;&nbsp;private int currentIndex = 0;public ObjectList(int size){&nbsp; &nbsp; this.size = size;&nbsp; &nbsp; objects = new Object[size];}&nbsp;private boolean isFull() {&nbsp; &nbsp; if(currentIndex == size)&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp;}&nbsp;public String add (java.lang.Object object){&nbsp; &nbsp; if ( ! isFull() ) {&nbsp; &nbsp; &nbsp; &nbsp; objects[currentIndex] = object;&nbsp; &nbsp; &nbsp; &nbsp; currentIndex++;&nbsp; &nbsp; &nbsp; &nbsp; return "Object added";&nbsp; &nbsp; }&nbsp; &nbsp; return "List full : object not added";}public void remove (int index){&nbsp; &nbsp; if( !isEmpty() ) {&nbsp; &nbsp; &nbsp; &nbsp; //shifting all the object to the left of deleted object 1 index to the left to fill the empty space&nbsp; &nbsp; &nbsp; &nbsp; for (int i = index; i < size - 1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objects[i] = objects[i + 1];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; currentIndex--;&nbsp; &nbsp; }}public boolean isEmpty(){&nbsp; &nbsp; if(currentIndex == 0)&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; return false;}public int getTotal(){&nbsp; &nbsp; return currentIndex;}public java.lang.Object getObject(int index){&nbsp; &nbsp; if(index < currentIndex)&nbsp; &nbsp; &nbsp;return objects[index];&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; null;}}

函数式编程

您似乎想要实现的是“扩展” ArrayList的功能并创建自定义对象列表。您可以做的是创建一个扩展 ArrayList 的类并定义/覆盖您想要的任何其他方法。public class ObjectList extends ArrayList<Object> {//constructor with initial capacityprivate int length;public ObjectList(int size){&nbsp; &nbsp;super(size);&nbsp; &nbsp;this.length= size;}public Object getObject(int index){&nbsp; &nbsp; return this.get(index);}}现在您拥有从 ArrayList 类和getObject方法继承的添加和删除函数。关于isFull方法,您可以检查 ObjectList 类的大小是否等于它被实例化的大小if(this.size() == this.length){return true}return false;并获得总计public int getTotal(){return this.size();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java