不熟悉JAVA或异常处理.寻找关于什么是可接受的和什么是不赞成的建议.
这个场景,我正在构建一个生命游戏程序,我设置了条件来检查一个单元格是否会超出界限而不是尝试访问该"单元格".我的问题是,使用try catch块而不是8个条件是否可以接受,如果抛出arrayOutOfBounds异常则不执行任何操作.即忽略细胞超出界限,或者这是不好的做法?例如...
try{
neighbors += cellIsAlive(row, col);
}catch(ArrayIndexOutofBoundsException e)
{
//dont do anything and continue counting neighbors
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,cellIsAlive方法检查多维数组中的位置,如果它处于活动状态,则返回1,否则抛出ArrayIndexOutofBoundsException.
这是一个好主意还是以这种方式使用异常是不好的做法?
提前感谢任何输入.
有可能在一个集合/ json中获取所有细节(用户,帖子,postimages)?
user-> hasmany(posts)post-> hasmany(postimage)
user:id | 名称
发布:id | user_id | 文本
postimage:id | post_id | imgpath
用户模型:
public function posts() {
return $this->hasMany('App\posts')->orderBy('id', 'ASC');
}
Run Code Online (Sandbox Code Playgroud)
帖子模型:
public function images(){
return $this->hasMany('App\postsimages');
}
Run Code Online (Sandbox Code Playgroud)
从用户获取所有帖子工作正常:
$myposts = users::find(Auth::user()->id)->posts;
Run Code Online (Sandbox Code Playgroud)
我能够从循环内的帖子中获取所有图像
foreach($myposts as $mypost) {
$postimages = $mypost->images;
}
Run Code Online (Sandbox Code Playgroud)
我想要的是获得所有帖子,没有循环的图像,例如
$myposts = users::find(Auth::user()->id)->posts->images
Run Code Online (Sandbox Code Playgroud)
谢谢
我根据一个等式生成一个数字数组,然后四舍五入到最接近的100.
之后,我想摆脱重复,array_unique似乎是这种情况的自然选择,但没有按预期工作.
我创建了一个小样本来证明这一点.PHP代码如下:
var_dump($amounts);
array_unique($amounts);
var_dump($amounts);
Run Code Online (Sandbox Code Playgroud)
结果是:
array(6) {
[0]=>
float(200)
[1]=>
float(300)
[2]=>
float(300)
[3]=>
float(400)
[4]=>
float(500)
[5]=>
float(500)
}
array(6) {
[0]=>
float(200)
[1]=>
float(300)
[2]=>
float(300)
[3]=>
float(400)
[4]=>
float(500)
[5]=>
float(500)
}
Run Code Online (Sandbox Code Playgroud)
有人可以对这里发生的事情有所了解吗?