自定义链接列表 - 无法将对象类转换为对象

时间:2010-10-27 06:13:14

标签: java object casting superclass

对于这个项目,我不允许使用泛型类。我已经尝试将我的所有课程复制到另一台朋友计算机上,他也会遇到同样的错误。

我无法拨打电话......

int counter = ((Movie) movieList.get(movListIndex)).getShowList().size();

其中getShowList()是我的Movie类中返回LinkedList的方法。

Eclipse说:

  

无法从对象到电影结束

此外

movieList.add(objMovie, counter);

其中objMovie是一个Movie对象。

Eclipse说:

  

LinkedList类型中的方法add(Object,int)不适用于参数(Movie,int)

-Suggestion是改变添加方法以获取电影。

我需要能够保持LinkedList和Node的方式,它们可以接收任何Object,当我创建一个Movie类时,我无法转换为Object。

链接列表类

public class LinkedList {
 Node head;
 public LinkedList() {
  head = null;
 }

 public boolean isEmpty() {
  return(head==null);
 }

 public int size() {
  int count = 0;
  Node current = head;
  while (current != null) {
   count++;
   current = current.getNext();
  }
  return count;
 }

 public Object get(int index) {
  if ((index < 0) || (index > size()))
   throw new IndexOutOfBoundsException();
  Node current = head;
  int count = 0;
  while (count > 0) {
   count ++;
   current = current.getNext();
  }
  return current.getData();
 }

 public void add (Object data, int index) {
  if ((index < 0) || (index > size())){
   throw new IndexOutOfBoundsException();
  }
  Node previous = null;
  Node current = head;
  while (index > 0) {
   index --;
   previous = current;
   current = current.getNext();
  }

  Node splice = new Node();
  splice.setData(data);
  splice.setNext(current);

  if (previous == null)
   head = splice;
  else
   previous.setNext(splice);
 }

 public Object remove (int index) {
  if ((index < 0) || (index >= size()))
   throw new IndexOutOfBoundsException();
  Node previous = null;
  Node current = head;
  while (index > 0) {
   index --;
   previous = current;
   current = current.getNext();
  }
  if (previous == null)
   head = current.getNext();
  else
   previous.setNext(current.getNext());

  return current.getData();
 }
}

节点类

public class Node {
 private Object data;
 private Node next;

 public Object getData() {
  return data;
 }
 public Node getNext() {
  return next;
 }
 public void setData(Object data) {
  this.data = data;
 }
 public void setNext(Node next) {
  this.next = next;
 }
}

如果需要电影类

public class Movie{
 private int filmNumber; //Movie ID
 private String filmTitle; //Title of Movie
 private int filmTime; //Movie Runtime
 private String filmRating; //Movie Rating
 private LinkedList showingList = new LinkedList();

 public Movie (int filmNumber, String filmTitle, int filmTime, String filmRating) {
  this.filmNumber = filmNumber; //sets global variable to parameter being passed
  this.filmTitle = filmTitle;   //                    |
  this.filmTime = filmTime;     //                    |
  this.filmRating = filmRating; //                    V
 }

 public int getFilmNumber() {
  return filmNumber; 
 }

 public String getFilmTitle() {
  return filmTitle;
 }

 public int getFilmTime(){
  return filmTime;
 }

 public String getFilmRating() {
  return filmRating;
 }

 public String getStringFilmTime() {
  String runtime = ""; //returned value
  int hours; //variable to hold 2 hour digits
  int minutes; //variable to hold minute digits

  hours = filmTime / 60; //formats original input to just hours
  minutes = filmTime % 60; //formats original input to just minutes

  if (minutes < 10) {
   runtime += ( Integer.toString(hours) + ":0" + Integer.toString(minutes));
   //Mod will only return a single digit if under 10
  }

  if (minutes > 10) {
   runtime += ( Integer.toString(hours) + ":" + Integer.toString(minutes));
   //Same statement without adding a '0'
  }

  return runtime;
 }

 public void setFilmNumber(int newFilmNumber) { 
  filmNumber = newFilmNumber;
 }

 public void setFilmTitle(String newFilmTitle) {
  filmTitle = newFilmTitle;
 }

 public void setFilmTime(int newFilmTime) {
  filmTime = newFilmTime;
 }

 public void setFilmRating(String newFilmRating) {
  filmRating = newFilmRating;
 }

 //public void setShowList()

 public LinkedList getShowList() {
  return showingList;
 }
}

1 个答案:

答案 0 :(得分:0)

这三个课程都有吗?我问,因为如果 - 由于某种原因 - 你的其他类旁边有一个名为Object的类,我可以重现错误信息。

可能很简单:

public class Object {
}

如果是这种情况,则不应将您的课程命名为ObjectStringInteger或其他任何内容java.lang