考虑一下代码:
public abstract class Item<T> implements Comparable<T>
{
protected T item;
public int compareTo(T o)
{
return 0; // this doesn't matter for the time being
}
}
public class MyItem<T> extends Item<String>
{
T object;
}
public class Foo<T>
{
protected ArrayList<T> list;
}
public class Bar<V> extends Foo<MyItem<V>>
{
public void sort()
{
Collections.sort(list);
}
}
Run Code Online (Sandbox Code Playgroud)
排序调用给出错误:
绑定不匹配:类型集合的泛型方法sort(List <T>)不适用于参数(ArrayList <MyItem <T >>).推断类型MyItem <T>不是有界参数的有效替代<T extends Comparable <?超级T >>
为什么这是错的?
如果MyItem<V>
实施Comparable
那么为什么它不是替代品呢?
对不起,如果有人询问,但我觉得这个问题有点具体.
我使用加密AES算法,当我加密16字节(一个块)时,结果是32字节.这个可以吗?
我使用的源代码是:
package net.sf.andhsli.hotspotlogin;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* Usage:
* <pre>
* String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
* ...
* String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
* </pre>
* @author ferenc.hechler
*/
public class SimpleCrypto {
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] …
Run Code Online (Sandbox Code Playgroud) 我有以下代码来获取当前日期之前的最后一个星期日:
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.WEEK_OF_YEAR, calendar.get(Calendar.WEEK_OF_YEAR)-1);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
Log.e("first day", String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
Run Code Online (Sandbox Code Playgroud)
但是这段代码不起作用.请告诉我,我该如何解决?
Java中的SecretKey
vs SecretKeySpec
类之间有什么区别?
文件SecretKeySpec
说:
它可以用于从字节数组构造一个SecretKey
在这段代码中,如果我打印secretKey.getEncoded()
或secret.getEncoded()
以十六进制表示,则两者都给出相同的输出.那我们为什么需要SecretKeySpec
呢?
final String password = "test";
int pswdIterations = 65536 ;
int keySize = 256;
byte[] ivBytes;
byte[] saltBytes = {0,1,2,3,4,5,6};
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec spec = new PBEKeySpec(
password.toCharArray(),
saltBytes,
pswdIterations,
keySize
);
SecretKey secretKey = factory.generateSecret(spec);
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(),"AES");
Run Code Online (Sandbox Code Playgroud)
以下是两次调用的输出getEncoded()
:
00367171843C185C043DDFB90AA97677F11D02B629DEAFC04F935419D832E697
我已经看到使用这种模式的两段Go代码:
type SomeType struct{
Field1 string
Field2 bool
_ struct{} // <-- what is this?
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释这段代码完成了什么?
Eclipse Checkstyle插件提供了两种自动更正问题的方法:
右键单击单个源文件,然后选择"应用Checkstyle更正".
右键单击问题标记,然后选择"快速修复".
如果我选择多个相同类型的标记并尝试快速修复所有标记,则Eclipse指出:
所选问题没有常见的适用快速修复方法.
有没有办法让我以更有效的方式执行相同类型的多个Checkstyle修正?
(参见相关问题:如何轻松修复Checkstyle错误?)
举个例子,考虑以下代码:
/**
* Example to demonstrate check-style quick fix.
*/
public final class CheckStyle {
/**
* Example constructor with no if braces.
*
* @param arg an argument
*/
public CheckStyle(final String arg) {
if (arg == null)
throw new RuntimeException("Arg is null.");
if (arg.isEmpty())
throw new RuntimeException("Arg is empty.");
}
}
Run Code Online (Sandbox Code Playgroud)
根据Sun的checkstyle配置检查时,会收到三个错误:
如果我选择与缺少大括号相关的两个错误并尝试快速修复它们,那么它将无法工作.
我在接受采访时被问到这个问题.
我有一个方法说public int add(int i, int j)
,这个方法已被许多客户使用.
现在我必须对add方法进行更新(可能是一些增强),这会创建一个我必须抛出异常的场景.如何让现有客户端继续使用该add()
方法而无需更改代码?
[访问者提示:客户可能会也可能不会使用我在添加方法中所做的任何新增强]
首先,我想过重载add,换行添加一个新的add方法,抛出异常.然后我想RuntimException
从添加中抛出异常......
但他们都没有接受为正确的方法.
我缺少任何模式或设计方法?
我有一个Java应用程序,它有工作线程来处理作业.一个worker产生一个结果对象,比如说:
class WorkerResult{
private final Set<ResultItems> items;
public Worker(Set<ResultItems> pItems){
items = pItems;
}
}
Run Code Online (Sandbox Code Playgroud)
当工人完成时,它执行此操作:
...
final Set<ResultItems> items = new SomeNonThreadSafeSetImplSet<ResultItems>();
for(Item producedItem : ...){
items.add(item);
}
passToGatherThread(items);
Run Code Online (Sandbox Code Playgroud)
这套items
装置在这里是一种"工作单元".该passToGatherThread
方法将items
集合传递给一个收集线程,其中只有一个在运行时存在.
这里不需要同步,因为竞争条件不会发生,因为只有一个线程(Gather-thread)读取该items
集合.AFAICS,Gather-thread可能看不到所有项目,因为该集合不是线程安全的,对吧?
假设我无法进行passToGatherThread
同步,因为它是第三方库.我基本上担心的是收集线程由于缓存,VM优化等而没有看到所有项目.所以这里出现了一个问题:如何以线程安全的方式传递项目集,以便Gather线程"看到"适当的项目?
我想让我的应用用户指定一个时区,然后在每个请求的开头设置他们的时区.在Rails,AFAICT中,这是通过设置单例对象来完成的:
Time.zone = "America/Los_Angeles"
Run Code Online (Sandbox Code Playgroud)
我对最佳实践的理解是,一般来说,应该将单例设置为ONCE.例如,在应用程序配置中.
从类似问题的答案,有人建议在ApplicationController中设置它
class ApplicationController < ActionController::Base
before_filter :set_timezone
def set_timezone
# current_user.time_zone #=> 'London'
Time.zone = current_user.time_zone if current_user && current_user.time_zone
end
end
Run Code Online (Sandbox Code Playgroud)
这样安全吗?或者我冒一个线程影响另一个线程的风险?我将尝试对此进行测试,但这不是最简单的测试场景,所以我想我应该看看是否有人已经这样做了.
java ×8
aes ×1
android ×1
arguments ×1
calendar ×1
checkstyle ×1
collections ×1
cryptography ×1
eclipse ×1
encryption ×1
exception ×1
generics ×1
go ×1
javaagents ×1
jce ×1
refactoring ×1
secret-key ×1
singleton ×1
struct ×1