我使用此代码将a转换Set
为List
:
Map<String, List> mainMap = new HashMap<String, List>();
for(int i=0; i<something.size(); i++){
Set set = getSet(...); //returns different result each time
List listOfNames = new ArrayList(set);
mainMap.put(differentKeyName,listOfNames);
}
Run Code Online (Sandbox Code Playgroud)
我想避免在循环的每次迭代中创建一个新列表.那可能吗?
通常情况下,我看到人们使用这样的类文字:
Class<Foo> cls = Foo.class;
Run Code Online (Sandbox Code Playgroud)
但是,如果类型是通用的,例如List?这工作正常,但有一个警告,因为List应该参数化:
Class<List> cls = List.class
Run Code Online (Sandbox Code Playgroud)
那么为什么不加一个<?>
呢?好吧,这会导致类型不匹配错误:
Class<List<?>> cls = List.class
Run Code Online (Sandbox Code Playgroud)
我认为这样的东西可行,但这只是一个简单的'语法错误:
Class<List<Foo>> cls = List<Foo>.class
Run Code Online (Sandbox Code Playgroud)
我如何Class<List<Foo>>
静态获取,例如使用类文字?
我可以用@SuppressWarnings("unchecked")
摆脱造成在第一个例子中的非参数使用目录的警告,Class<List> cls = List.class
但我宁愿不要.
有什么建议?
在Visual Studio中编码期间,我得到了一个未解决的外部符号错误,我不知道该怎么做.我不知道出了什么问题.你能破译我吗?我应该在哪里寻找什么样的错误?
1>Form.obj : error LNK2019: unresolved external symbol "public: class Field * __thiscall Field::addField(class Field *)" (?addField@Field@@QAEPAV1@PAV1@@Z) referenced in function "public: void __thiscall Form::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Form@@QAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall Field::parse(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?parse@Field@@UAEXAAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __thiscall InputField::InputField(class std::basic_stringstream<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0InputField@@QAE@AAV?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>Form.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Field::prompt(void)" (?prompt@Field@@UAEXXZ)
1>Form.obj : error LNK2001: unresolved external …
Run Code Online (Sandbox Code Playgroud) 我将收到一个JSON编码的字符串形式Obj-C,我正在解码一个虚拟字符串(现在),如下面的代码.我的输出带有每个项目前缀字符'u':
[{u'i': u'imap.gmail.com', u'p': u'aaaa'}, {u'i': u'333imap.com', u'p': u'bbbb'}...
Run Code Online (Sandbox Code Playgroud)
JSON如何添加这个unicode char?删除它的最佳方法是什么?
mail_accounts = []
da = {}
try:
s = '[{"i":"imap.gmail.com","p":"aaaa"},{"i":"imap.aol.com","p":"bbbb"},{"i":"333imap.com","p":"ccccc"},{"i":"444ap.gmail.com","p":"ddddd"},{"i":"555imap.gmail.com","p":"eee"}]'
jdata = json.loads(s)
for d in jdata:
for key, value in d.iteritems():
if key not in da:
da[key] = value
else:
da = {}
da[key] = value
mail_accounts.append(da)
except Exception, err:
sys.stderr.write('Exception Error: %s' % str(err))
print mail_accounts
Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList
,我想完全复制它.我假设有人花了一些时间使其正确,我尽可能使用实用程序类.很自然地,我最终得到了Collections
一个包含复制方法的类.
假设我有以下内容:
List<String> a = new ArrayList<String>();
a.add("a");
a.add("b");
a.add("c");
List<String> b = new ArrayList<String>(a.size());
Collections.copy(b,a);
Run Code Online (Sandbox Code Playgroud)
这失败了,因为基本上它认为b
不够大a
.是的,我知道b
它的大小为0,但它现在应该足够大,不应该吗?如果我必须先填补b
,那么Collections.copy()
在我的脑海中就会成为一个完全没用的功能.所以,除了编写一个复制函数(我现在要做的)之外,还有一种正确的方法吗?
我目前正在使用jackson 2.1.4,当我将对象转换为JSON字符串时,我在忽略字段时遇到了一些麻烦.
这是我的类,它充当要转换的对象:
public class JsonOperation {
public static class Request {
@JsonInclude(Include.NON_EMPTY)
String requestType;
Data data = new Data();
public static class Data {
@JsonInclude(Include.NON_EMPTY)
String username;
String email;
String password;
String birthday;
String coinsPackage;
String coins;
String transactionId;
boolean isLoggedIn;
}
}
public static class Response {
@JsonInclude(Include.NON_EMPTY)
String requestType = null;
Data data = new Data();
public static class Data {
@JsonInclude(Include.NON_EMPTY)
enum ErrorCode { ERROR_INVALID_LOGIN, ERROR_USERNAME_ALREADY_TAKEN, ERROR_EMAIL_ALREADY_TAKEN };
enum Status { ok, error };
Status status; …
Run Code Online (Sandbox Code Playgroud) 关于神经网络理论,这是一个主要问题:
为什么我们必须规范化神经网络的输入?
我理解有时,例如当输入值是非数字时,必须执行某个转换,但是当我们有数字输入时?为什么数字必须在一定的间隔内?
如果数据未规范化会发生什么?
因为我一直在使用Spring,如果我要编写一个具有依赖项的服务,我会执行以下操作:
@Component
public class SomeService {
@Autowired private SomeOtherService someOtherService;
}
Run Code Online (Sandbox Code Playgroud)
我现在遇到了使用另一个约定来实现相同目标的代码
@Component
public class SomeService {
private final SomeOtherService someOtherService;
@Autowired
public SomeService(SomeOtherService someOtherService){
this.someOtherService = someOtherService;
}
}
Run Code Online (Sandbox Code Playgroud)
我理解这两种方法都有效.但是使用选项B有一些优势吗?对我来说,它在类和单元测试中创建了更多代码.(必须编写构造函数而不能使用@InjectMocks)
有什么我想念的吗?除了在单元测试中添加代码之外,还有其他任何自动装配的构造函数吗?这是一种更优先的依赖注入方式吗?
我正在学习Spring 3的注释fu的绳索,我偶然发现了newb的噩梦异常.非常感谢任何帮助.
这里是jsp代码的形式:
<form:form method="POST" action="login.htm" modelAttribute="login">
....
<form:input path="email" size="20" />
....
Run Code Online (Sandbox Code Playgroud)
控制器代码(此时我只是测试水域,所以不指向任何页面,只返回一个空字符串):
@Controller
@SessionAttributes
public class LoginController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login( @ModelAttribute("login") Login login,
BindingResult result) {
System.out.println(" email entered "+ login.getEmail()+ "\n");
return "test";
}
Run Code Online (Sandbox Code Playgroud)
"Login"是一个带有相应setter和getter的表单bean.
我假设我的调度程序servlet中的这段代码应该处理注释扫描:
<context:component-scan
base-package="com.testAnnFu.controller" />
Run Code Online (Sandbox Code Playgroud)
这是我尝试加载登陆jsp页面时抛出的可耻异常.
SEVERE: Neither BindingResult nor plain target object for bean name 'login' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'login' available as request attribute …
Run Code Online (Sandbox Code Playgroud) 如何动态循环遍历java中的类属性.
例如:
public class MyClass{
private type1 att1;
private type2 att2;
...
public void function(){
for(var in MyClass.Attributes){
System.out.println(var.class);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这在Java中可能吗?
java ×5
json ×2
list ×2
annotations ×1
attributes ×1
autowired ×1
c++ ×1
class ×1
collections ×1
constructor ×1
copy ×1
generics ×1
jackson ×1
literals ×1
loops ×1
performance ×1
python ×1
set ×1
spring ×1
spring-mvc ×1
visual-c++ ×1