Java 将对象添加到对象数组
目标:如果有添加空间,则将新电影对象添加到现有电影[] 代码: 方法代码:Java 将对象添加到对象数组,java,arrays,object,Java,Arrays,Object,目标:如果有添加空间,则将新电影对象添加到现有电影[] 代码: 方法代码: public static int addMovie (Movie[] movieArray, Movie addMe, int count) { if (count != movieArray.length) { count++; movieArray[count] = addMe; System.out.println("Movie added
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数组打印出来时,新条目打印为null,即使创建的Movie对象将在路外正常打印。因此,我假设将addMe对象添加到数组中的最佳方法是创建在数组中初始化的第二个新电影对象,并逐块构建它(这样addMe将保留在内存中,addMe的“副本”将被设置到数组中)
这对我来说不是很有效率(我讨厌额外的数据…)。有更好的方法吗
注意:电影对象实际上有10个私有数据成员。对于本练习,我只需要传入两个参数,并为其余参数设置默认值。您可以想象为什么我不使用十个GET语句来构建这个数组,并在内存中保留额外的对象
编辑:
当前打印输出(部分):
更多编辑:
构造函数和Setters代码-所有这些都应该正常工作
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;
}
公共电影(字符串t,int y)
{
//传入
this.title=setTitle(t);
本年=设定年(y);
//默认值
这个排名=0;
this.director=“无董事”;
this.actors=“无参与者”;
这个。奥斯卡=0;
this.linkIMDB=“无IMDB链接”;
this.linkGuardian=“无监护人链接”;
this.country=“无国家”;
this.genre=“无流派”;
}
公共字符串setTitle(字符串newTitle)
{
如果(newTitle==null)
{
this.title=“无标题”;
}
其他的
{
this.title=newTitle;
}
返回此.title;
}
公共整数设定年(整数新年)
{
如果(新年>=999&&newYear不清楚您在问什么,但这部分不正确:
count++;
movieArray[count] = addMe;
如果movieArray.length
是10,而count
是9,那么它将通过count!=movieArray.length
检查,然后您将尝试在索引10处分配元素。使用后期增量:
movieArray[count++] = addMe;
if (count <= movieArray.length)
{
movieArray[count] = addMe;
count++;
System.out.println("Movie added successfully!");
}
明白了
我使用Count设置存储新电影的索引。
最初的计数是176。
最后一个指数是175。
我在设置电影之前是递增的,所以电影被设置为索引177。
所以176被跳过了
它只打印到176,因为这是实际的计数,而不是跳过的空间(数组中有一个额外的对象没有打印)
(当我尝试向数组添加2个新的电影对象时,我发现了这一点,得到了一个空值,然后第一个对象只打印出来)
通过切换集合和增量来解决:
movieArray[count++] = addMe;
if (count <= movieArray.length)
{
movieArray[count] = addMe;
count++;
System.out.println("Movie added successfully!");
}
if(count“当movieList数组打印出来时,新条目打印为null”What?Where?How?此代码看起来不错。YuriySystem.out.println
是一个很好的提示;)为什么要在检查计数是否与数组长度不相等后增加计数?这不是问题所在。按照这种思路,new
运算符也只返回一个“内存地址”而不是对象本身,那么复制的意义是什么呢?我认为这可能是一个你必须自己去探究的问题。构造更简单的测试用例,在代码中设置断点,并尝试看看为什么你会得到null。好吧,正确的搜索不应该引发任何异常,所以这并不能真正说明数组的多少。我真的认为您做的假设太多。最好确定并使用IDE的调试工具。Count将始终小于数组大小,因为它是最初从文件读入电影时生成的一个数字。请参阅上面的注释。@ChristinaKline这不是唯一的结果;您的代码也永远不会分配给第一个元素ndex 0。该程序首先从八个不同的文件加载176部电影。我只想能够添加到此列表中。任务是首先创建一个对象,然后将该对象传递到一个方法中,该方法将其添加到现有的电影列表中。它正确打印-除了添加的电影打印为Null,而不是打印信息。我已经从输出中添加了一个片段。176是新的索引号-这是正确的。但是之前的信息是空的。明白了!我正在使用Count设置存储新电影的索引。原始计数是176。最后一个索引是175。我在设置电影之前是递增的,所以电影被设置为索引177。所以176被跳过。它只打印到176,因为这是实际计数,而不是跳过的空间。(当我尝试向数组中添加2个新的电影对象时,我发现了这一点,得到了一个空值,然后只有第一个对象)。通过切换集合和增量来解决。这与@ZongZhengLi的效果相同,因为count++
的值是count
的旧值。这是问题的关键。