目标:如果有空间可以添加,则将新的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
=============================================================================
更多编辑: 构造函数和Setter代码 - 所有这些应该正常工作。
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;
}
答案 0 :(得分:1)
目前尚不清楚你在问什么,但这部分不正确:
count++;
movieArray[count] = addMe;
如果movieArray.length
为10,count
为9,该怎么办?然后它将通过count != movieArray.length
检查,然后您将尝试在索引10处分配元素。使用后增量:
movieArray[count++] = addMe;
答案 1 :(得分:0)
GOT IT!
我使用Count来设置存储新电影的索引。 原计数是176。 最后一个指数是175。 在设置电影之前我是增量,因此电影正在索引177处设置。 所以176被忽略了。
它只打印到176,因为这是实际计数,这不计算跳过的空间(数组中有一个额外的对象没有被打印)。
(当我尝试向数组中添加2个新的Movie对象并获得一个null然后仅在打印时出现第一个对象时计算出来。)
通过切换设置和增量来解决:
if (count <= movieArray.length)
{
movieArray[count] = addMe;
count++;
System.out.println("Movie added successfully!");
}