我生成了两个1000x 矩阵1000:
第一矩阵:O和#.
第二个矩阵:O和B.
使用以下代码,第一个矩阵需要8.52秒才能完成:
Random r = new Random();
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if(r.nextInt(4) == 0) {
System.out.print("O");
} else {
System.out.print("#");
}
}
System.out.println("");
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,第二个矩阵需要259.152秒才能完成:
Random r = new Random();
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) …Run Code Online (Sandbox Code Playgroud) 我只是整理了我的代码并且有这篇文章:
String saving = getValue();
if(saving != null && saving.equals("true")){
// do something
}
Run Code Online (Sandbox Code Playgroud)
然后我想到了另一种方法来摆脱检查null:
if("true".equals(saving)){
// do something
}
Run Code Online (Sandbox Code Playgroud)
它确实有效,但这样做是否安全?我的意思是字符串文字存储在公共池中,而字符串对象create by存储在new堆上.但是常量池中的字符串也是对象,对吧?
但即使它使代码更短,它似乎也不是正确的做法.
我试图理解C++中的常量引用,我偶然发现了这个问题:
当我将double赋值给const int&然后更改引用double的值时,my int引用的值保持不变.
double i = 10;
const int &ref = i;
i = 20;
cout << "i: " << i << endl; // i = 20
cout << "&ref: " << ref << endl; // ref = 10
Run Code Online (Sandbox Code Playgroud)
而在分配int时,值会发生变化.
int i = 10;
const int &ref = i;
i = 20;
cout << "i: " << i << endl; // i = 20
cout << "&ref: " << ref << endl; // ref = 20
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?我的猜测是,当分配double时,隐式转换为int会创建一个新对象,然后分配它的引用,但是我无法在任何地方找到它.
我有2个具有多对多关系的实体.电影实体是此关系的所有者,因此当我想删除一个Actor实体时,我使用注释@PreRemove的方法删除Movie Cast中出现的任何Actor ID,以避免"外键违规异常".
电影课
@Entity
public class Movie extends AbstractBusinessObject{
@ManyToMany
private Map<String, Actor> cast;
// setters and getters
public void removeCastMember(Actor actor){
for (Entry<String, Actor> e : cast.entrySet()) {
if(e.getValue().id.equals(actor.id)){
cast.remove(e.getKey());
}
}
} // removeCastMember()
}
Run Code Online (Sandbox Code Playgroud)
演员班
@Entity
public class Actor extends AbstractBusinessObject{
@ManyToMany(mappedBy = "cast")
private Set<Movie> movies;
// setters and getters
@PreRemove
private void removeActorFromMovies() {
for (Movie m : movies) {
m.removeCastMember(this);
}
}
}
Run Code Online (Sandbox Code Playgroud)
要清楚,从我的测试中,它可以工作 - 电影对象在数据库中正确更新.但是,我无法理解在没有调用saveOrUpdate()或者持久化/合并这些对象时怎么可能.
新的GCM 3.0应允许GCM自动显示从服务器发送的通知(如果它们包含notification参数).
正如文档中所述:
带有预定义选项的通知参数表示如果客户端应用程序在Android上实现GCMListenerService,GCM将代表客户端应用程序显示消息
但是,即使GCMListenerService已经实现,我也无法使其工作.
AndroidManifest.xml中
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="cz.kubaspatny.pushservertest" />
</intent-filter>
</receiver>
<service
android:name="cz.kubaspatny.pushservertest.gcm.CustomGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
CustomGcmListenerService.java
public class CustomGcmListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle extras) {
super.onMessageReceived(from, extras);
Log.d("GcmListenerService", "Received gcm from " + from + " with bundle " + extras.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
记录来自服务器的通知但GCM未显示.
Received gcm from 333813590000 with bundle Bundle[{notification={"icon":"ic_launcher.png","body":"great match!","title":"Portugal vs. Denmark"}, …Run Code Online (Sandbox Code Playgroud) 我试过谷歌但找不到答案.
如果我这样做
c = pickle.load(open(fileName, 'r'))
Run Code Online (Sandbox Code Playgroud)
此操作后文件会自动关闭吗?
我有2个实体 - 电影和演员 - 有M:N的关系.在为这些实体设计DTO对象时,我不确定什么是正确的方法.
电影
@Entity
@Table(name = "Movies")
public class Movie extends AbstractBusinessObject {
private String name;
private String short_info;
@ManyToMany
private Map<String, Actor> cast;
}
Run Code Online (Sandbox Code Playgroud)
演员
@Entity
@Table(name = "Actors")
public class Actor extends Person{
private String name;
@ManyToMany(mappedBy = "cast")
private Set<Movie> movies;
}
Run Code Online (Sandbox Code Playgroud)
现在关于DTO:我遇到了两种不同的方式来处理1:N和M:N关系.
仅保存ID:
public class MovieDto {
private String name;
private String short_info;
// Long represents Actor's ID
private Map<String, Long> cast;
}
Run Code Online (Sandbox Code Playgroud)
然而,正如我在这里所说的那样Instead of performing many remote calls on …
我一直在测试代码并偶然发现了一个问题:您是否应该在子类中调用可以覆盖超类方法super.equals()中使用的某些方法的方法?equals()
让我们考虑以下代码:
public abstract class Item {
private int id;
private float price;
public Item(int id, String name, float price, String category) {
this.id = id;
this.name = name;
this.price = price;
this.category = category;
}
public int getID() {
return id;
}
public float getPrice() {
return price;
}
@Override
public boolean equals(Object object){
if(object instanceof Item){
Item item = (Item) object;
if( id == item.getID()
&& price == item.getPrice())
{ return true; }
}
return …Run Code Online (Sandbox Code Playgroud) 根据文档InstanceId:
为每个应用程序实例提供唯一标识符
并且实例 ID 是稳定的,但在以下情况下可能会变得无效:
- 应用程序删除实例ID
- 设备已恢复出厂设置
- 用户卸载应用程序
- 用户清除应用数据
然而,根据我的测试,即使应用程序仅从最近的应用程序中刷过,以下调用似乎也会返回不同的 ID。
InstanceID.getInstance(context).getID();
Run Code Online (Sandbox Code Playgroud)
这是 Google Play 服务中的错误还是我使用它完全错误?
我正在尝试编写一个查询(PostgreSQL)来获得"2012年奖项数量最多的电影".
我有以下表格:
CREATE TABLE Award(
ID_AWARD bigserial CONSTRAINT Award_pk PRIMARY KEY,
award_name VARCHAR(90),
category VARCHAR(90),
award_year integer,
CONSTRAINT award_unique UNIQUE (award_name, category, award_year));
CREATE TABLE AwardWinner(
ID_AWARD integer,
ID_ACTOR integer,
ID_MOVIE integer,
CONSTRAINT AwardWinner_pk PRIMARY KEY (ID_AWARD));
Run Code Online (Sandbox Code Playgroud)
我写了下面的查询,它给出了正确的结果,但我认为有很多代码重复.
select * from
(select id_movie, count(id_movie) as awards
from Award natural join awardwinner
where award_year = 2012 group by id_movie) as SUB
where awards = (select max(count) from
(select id_movie, count(id_movie)
from Award natural join awardwinner
where award_year = 2012 group by …Run Code Online (Sandbox Code Playgroud) java ×5
android ×2
c++ ×1
comparison ×1
const ×1
dto ×1
equals ×1
for-loop ×1
hibernate ×1
inheritance ×1
int ×1
jpa ×1
loops ×1
performance ×1
pickle ×1
postgresql ×1
python ×1
sql ×1
string ×1
system.out ×1