Java 无法检查ArrayList的元素<;对象>;如果它在Android中有空值
我有一个Java 无法检查ArrayList的元素<;对象>;如果它在Android中有空值,java,android,arraylist,Java,Android,Arraylist,我有一个数组列表,我的目标游戏包括日期,对手,得分和其他包含10个元素的字段 有时Score可能null 如何检查并将其更改为某个默认值 我尝试了以下方法: for(Game a : arrList) { if(a.getScore() == null) { } } 我需要做如果(…)10次,或者有其他更快的方法?在你的课堂游戏中,你可以为分数设置一个默认值: class Game{ private Score score; public Score ge
数组列表
,我的目标游戏包括日期
,对手
,得分
和其他包含10个元素的字段
有时Score
可能null
如何检查并将其更改为某个默认值 我尝试了以下方法:
for(Game a : arrList)
{
if(a.getScore() == null)
{
}
}
我需要做
如果(…)
10次,或者有其他更快的方法?在你的课堂游戏中,你可以为分数设置一个默认值:
class Game{
private Score score;
public Score getScore(){
return this.score == null? this.score : new Score();
}
}
供您参考:
每个类变量、实例变量或数组组件都是
创建时使用默认值初始化(§15.9,§15.10.2):
对于byte类型,默认值为零,即
(字节)0
对于type short,默认值为零,即
(短)0
对于int类型,默认值为零,即0
对于long类型,默认值为零,即0L
对于类型float,默认值为正零,即0.0f
对于double类型,默认值为正零,即0.0d
对于char类型,默认值为空字符,即,
“\u0000”
对于boolean类型,默认值为false
对于所有引用类型(§4.3),默认值为空。
在课堂游戏中,您可以为分数设置默认值:
class Game{
private Score score;
public Score getScore(){
return this.score == null? this.score : new Score();
}
}
编辑:
class Game{
private Score score;
public Score getScore(){
return this.score;
}
public void setScore(Score score){
this.score = score;
}
public Score getScoreOrDefault(Score default){
if(Objects.isNull(this.score)){
setScore(default);
return default;
}
}
}
在您可以调用getScoreOrDefault将新分数作为参数传递之后:我认为您这样做是正确的,但是如果您想要更高的性能,那么您可以在
循环中使用三元运算符,如下所示
(a.getScore()==null)? a.setScore("value"):do nothing;
您可以更新Game
中的getScore()
方法以返回可选的:
示例
public class Game {
private Score score;
private String name;
public Game(String name) { this.name = name;}
public Game(String name, Integer scoreVal) {
this.name = name;
score = new Score(scoreVal);
}
public String getName() { return name; }
public Optional<Score> getScore() {
return Optional.ofNullable(score);
}
public static void main(String[] args) {
List<Game> games = new ArrayList<Game>();
games.add(new Game("Game 1"));
games.add(new Game("Game 2", 10));
for(Game game: games) {
game.getScore().ifPresent(score ->
System.out.println("Score value in " + game.getName() + " is " + score.getValue()));
}
}
}
class Score {
private Integer value = 0;
public Score(Integer val) { value = val; }
public Integer getValue() { return value; }
}
当你在游戏
类中声明分数时,你不能将分数初始化为默认值吗?除非你能在游戏类中用默认值初始化分数属性,否则没有办法改进你的代码。你能详细解释一下“如果(…)做10次”的意思吗?也许你可以让Game.getScore
返回一个可选的,然后使用a.getScore().orElse(defaultValue)
而不是a.getScore()
。甚至引入一个Game.getScore(defaultValue)
getter。为什么迭代10个元素会让你烦恼?没有理由过度设计它。@user1506104,不,Score
是一个类。作为此解决方案的扩展,并且由于您可能希望保持pojo行为尽可能清晰,请添加一些getter方法,如getScoreOrDefault(Score默认值):Score。您是对的,我在您的评论之后编辑了我的代码。我认为这与简单的“如果”相比没有任何性能改进,两者都编译为相同的java字节码。@marmor我读了它,然后。如果你有更多的推荐人,我会很想知道的
public class Game {
private Score score;
private String name;
public Game(String name) { this.name = name;}
public Game(String name, Integer scoreVal) {
this.name = name;
score = new Score(scoreVal);
}
public String getName() { return name; }
public Optional<Score> getScore() {
return Optional.ofNullable(score);
}
public static void main(String[] args) {
List<Game> games = new ArrayList<Game>();
games.add(new Game("Game 1"));
games.add(new Game("Game 2", 10));
for(Game game: games) {
game.getScore().ifPresent(score ->
System.out.println("Score value in " + game.getName() + " is " + score.getValue()));
}
}
}
class Score {
private Integer value = 0;
public Score(Integer val) { value = val; }
public Integer getValue() { return value; }
}
Score value in Game 2 is 10
Using Java 8 Syntax
via this way you can check in list which fastest
arrList
.stream()
.filter(p-> p.getscore()==null)