Java Object Array打印为null?

时间:2017-05-17 06:25:27

标签: java arrays null

我有3个类Movie,用于将Movie个对象添加到MovieDatabase中,但它会一直打印为空。

当我添加2 Movies时,它会删除第一个Movie,而是打印null。还有一种方法可以检查数组中的位置是否为空,如果它是空的则不打印?

这是我的Movie班级

public class Movie {

    private String name;  
    private String director;
    private double fileSize;               
    private int duration;
    private int moviecount;

    public Movie()
    {
        name = null;
        director = "";
        fileSize = 0;
        duration = 0;
    }

    public void setName(String newName)
    {
        name = newName;
    }

    public String getName()
    {
        return name;
    }

    public void setDirector(String newDirector)
    {
        director = newDirector;
    }

    public String getDirector()
    {
        return director;
    }

    public void setfileSize(double newfileSize)
    {
        fileSize = newfileSize;
    }

    public double getfileSize()
    {
        return fileSize;
    }

    public void setDuration(int newDuration)
    {
        duration = newDuration;
    }

    public int getDuration()
    {
        return duration;
    }

这里是我的MovieDatabase课程:

public class MovieDatabase
{
    private Movie[] mov;
    private int i;
    public int count=0;

    public MovieDatabase()
    {
        mov = new Movie[4];
        i=0;
    }

    public void addData(String name, String director, double fileSize, int duration)
    {
        for(int i=0; i<4; i++)
        mov[i] = new Movie();
        setData(mov[i],name,director,fileSize,duration);
        i++;
        count++;
    }
    private void setData(Movie m,String name, String director, double fileSize, int duration)
    {
            mov[i].setName(name);
            mov[i].setDirector(director);
            mov[i].setfileSize(fileSize);
            mov[i].setDuration(duration);
    }
    public void printNames()
    {
        for (int i = 0; i < mov.length; i++) 
        {
        System.out.println(mov[i].getName());
        }

    }
}





import java.util.*;
public class Interface {
Scanner console = new Scanner(System.in);
MovieDatabase m = new MovieDatabase();
private void run() 
{

int option;



    do{
        System.out.print("Add Movie(0), Delete Movie(2),Show Movies(3),Movie Count(4) \n");
        option = console.nextInt();

        switch(option)
        {
            case 0: addMovie();
                    break;

            case 3: printMovies();
                    break;



        }
    }        

        while(option!=9);

}
public static void main(String[] args){
 Interface intFace = new Interface();
intFace.run();
}

public void addMovie()
{
    String name, director;
    double fileSize; 
    int duration;

    System.out.println("Movie Name: ");
    name = console.next();
    System.out.println("Movie Director: ");
    director = console.next();
    System.out.println("Movie File Size: ");
    fileSize = console.nextDouble();
    System.out.println("Movie Duration: ");
    duration = console.nextInt();
    System.out.print("Movie Added!");


    m.addData(name,director,fileSize,duration);
}
public void printMovies()
{
    m.printNames();
}

}

我试图只包括相关部分,但到目前为止我所做的大部分内容都是相关的。

2 个答案:

答案 0 :(得分:2)

问题出在这些方面

....
public void addData(String name, String director, double fileSize, int duration)
{
    for(int i=0; i<4; i++)
    mov[i] = new Movie();
    ...

每次添加新数据时,您都会通过将新的电影对象分配给数组中的每个元素来删除所有以前的记录。这会删除以前的所有数据。

您应该在MovieDatabase构造函数中移动这两行。或者更好的选择是在您设置数据时初始化它们。

...
public void addData(String name, String director, double fileSize, int duration)
{
    setData(mov[i],name,director,fileSize,duration);
    i++;
    count++;
}
private void setData(Movie m,String name, String director, double fileSize, int duration)
{ 
        mov[i] = new Movie(); //++ edit
        mov[i].setName(name);
        mov[i].setDirector(director);
        mov[i].setfileSize(fileSize);
        mov[i].setDuration(duration);
}
...
  

还有办法检查数组中的位置是否为空,如果为空则不打印?

您可以在Movie类中创建一个方法,检查此影片对象是否为空并返回相应的结果。

public class Movie {
    ...
    ...
    public boolean isEmpty() {
        if(
            this.name.isEmpty() &&  
            this.director &&
            this.fileSize == 0 &&
            this.duration == 0 &&
            this.moviecount == 0
        )
            return true;
        else
            return false;
    }
    ...
    ...
}

现在,您可以使用以下方法检查此影片对象是否为空:

if(mov[i].isEmpty()) {
    //empty movie object
    ...
}

答案 1 :(得分:0)

setData中,您始终设置mov[0]的值。类成员i永远不会改变(循环变量隐藏它)。您不使用参数m来设置数据。

setData更改为

m.setName(name);
m.setDirector(director);
m.setfileSize(fileSize);
m.setDuration(duration);