繁体   English   中英

将对象添加到对象数组

[英]Adding an Object to an Object Array

目标:如果有添加空间,请向现有的Movie []中添加一个新的Movie对象。

码:

    // Create the new Movie object
    Movie movieToAdd = new Movie (newTitle, newYear);

    // Add it to the Array
    count = addMovie(movieList, movieToAdd, count);

方法代码:

    public static int addMovie (Movie[] movieArray, Movie addMe, int count)
{
    if (count != movieArray.length)
    {
        count++;
        movieArray[count] = addMe;
        System.out.println("Movie added successfully!");
    }
    else
    {
        System.out.println("Array size of " + movieArray.length + " is full. Could not add movie.");
    }


    return count;
}

问题:当前,当movieList数组被打印出来时,即使创建的Movie对象在输出过程中可以正常打印,新条目也将打印为null。 因此,我假设将addMe对象添加到数组中的最佳方法是创建在数组中初始化的第二个新Movie对象,并逐段构建(因此addMe将保留在内存中,并添加addMe的“副本”将被设置为数组)。

这对我来说不是很有效(我讨厌多余的数据...)。 有一个更好的方法吗?

注意:Movie对象实际上有10个私有数据成员。 对于本练习,我只需要传入两个参数并为其余参数设置默认值。 您可以想象为什么我不使用十个GET语句来构建此数组并将多余的对象卡在内存中的原因...

编辑:当前打印输出(部分):

    Menu options:
1.  Show all movies: 
2.  Show movies sorted - manual
3.  Show movies sorted - auto
4.  Show Movie by Index
5.  Search for movie Linearly
6.  Search for movie using Binary Search
7.  Add a movie
20. Quit 
Please choose an option from the menu: 1 to 20: 
7
Let's add the information for the new movie. Give me a Title and 4-digit Year, and I'll fill in the rest.
Title? 
Me
Year of Release? 
Please enter a valid 4 digit year: 1000 to 9999: 
1213
Movie added successfully!
Menu options:
1.  Show all movies: 
2.  Show movies sorted - manual
3.  Show movies sorted - auto
4.  Show Movie by Index
5.  Search for movie Linearly
6.  Search for movie using Binary Search
7.  Add a movie
20. Quit 
Please choose an option from the menu: 1 to 20:




25 | Les Vampires (1915)                   | Louis Feuillade                               | "Edouard Mathe, Marcel Levesque"                                                                                                                                                          | 1915 |   0 | http://www.imdb.com/title/tt0006206/          | http://www.guardian.co.uk/film/movie/117077/vampires                               | France       | Horror               | 175
null | 176
=============================================================================

更多编辑:构造函数和设置器代码-尽管所有这些都应该正常工作。

    public Movie (String t, int y)
{
//  passed in
    this.title = setTitle(t);
    this.year = setYear(y);

//  defaults
    this.ranking = 0;
    this.director = "No Director";
    this.actors = "No Actors";
    this.oscars = 0;
    this.linkIMDB = "No IMDB Link";
    this.linkGuardian = "No Guardian Link";
    this.country = "No Country";
    this.genre = "No Genre";    
}

    public String setTitle (String newTitle)
{       
    if (newTitle == null)
    {
        this.title = "No Title";
    }
    else
    {
        this.title = newTitle;
    }

    return this.title;
}

    public int setYear (int newYear)
{
    if (newYear >= 999 && newYear <=10000)
    {
        this.year = newYear;
    }
    else
    {
        newYear = 0000;
    }

    return this.year;
}

不清楚您要问的是什么,但此部分不正确:

count++;
movieArray[count] = addMe;

如果movieArray.length为10, count为9怎么办? 然后它将通过count != movieArray.length检查,然后您将尝试在索引10处分配该元素。请使用post增量:

movieArray[count++] = addMe;

得到它了!

我使用Count来设置新电影的存储索引。 原始计数是176。最后一个索引是175。在设置影片之前,我已进行了递增操作,因此将影片设置为索引177。因此跳过了176。

它只打印到176,因为这是实际计数,没有考虑跳过的空间(数组中有一个未打印的额外对象)。

(当我尝试向数组中添加2个新的Movie对象并得到null值,然后仅打印第一个对象时,发现了这一点)。

通过切换设置和增量来解决:

if (count <= movieArray.length)
    {
        movieArray[count] = addMe;
        count++;
        System.out.println("Movie added successfully!");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM