条件:不修改原始列表; 仅限JDK,没有外部库.单行或JDK 1.3版本的奖励积分.
有没有比以下更简单的方法:
List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);
Run Code Online (Sandbox Code Playgroud) 从Java 5开始,我们已经对原始类型进行了装箱/拆箱,因此它int
被包装成java.lang.Integer
等等.
我最近看到了很多新的Java项目(肯定需要一个至少版本为5的JRE,如果不是6个),int
而不是java.lang.Integer
使用后者,尽管使用后者要方便得多,因为它有一些帮助方法可以转换到long
的值等.
为什么有些人仍然在Java中使用原始类型?有什么实际好处吗?
我正在尝试运行Java程序,但它采用默认的GMT时区而不是OS定义的时区.我的JDK版本是1.5,操作系统是Windows Server Enterprise(2007)
Windows指定了一个中央时区,但是当我运行以下程序时,它会给我一个GMT时间.
import java.util.Calendar;
public class DateTest
{
public static void main(String[] args)
{
Calendar now = Calendar.getInstance();
System.out.println(now.getTimeZone());
System.out.println(now.getTime());
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出
sun.util.calendar.ZoneInfo[id="GMT",
offset=0,
dstSavings=0,
useDaylight=false,
transitions=0,
lastRule=null]
Mon Mar 22 13:46:45 GMT 2010
Run Code Online (Sandbox Code Playgroud)
请注意,我不想从应用程序设置时区.我希望JVM使用的时区应该是操作系统中指定的时区.(我没有发现其他具有1.4版JDK和Microsoft Server 2003的服务器的问题).
任何想法都将受到高度赞赏.
如何将详细垃圾收集输出重定向到文件?Sun的网站显示了Unix的一个例子,但它不适用于Windows.
什么是可能受2014年5月Oracle v Google决策影响的37个Java API包?
上诉法院认为对该语言至关重要的3个套餐是什么?
如何避免在Java代码中使用受限制的API?
如果社区要创建一个竞争的开放类路径它会是什么样子?
更新:截至2016年5月,谷歌使用这些API已被统一使用.http://arstechnica.com/tech-policy/2016/05/google-wins-trial-against-oracle-as-jury-finds-android-is-fair-use/
我可以发誓,就在几个月前,我下载了Java 1.5 SE JDK 的副本,而且我没有向他们提供有关我第一次出生的信息.今天,我必须通过注册 - 我们将发送你 - 链接 - 有朝一日的舞蹈.我还没有收到链接,所以我想我会在这里问一下.
最后,我在美国,所以我不必担心出口限制.
有什么想法吗?
PS我是否提到我正在尝试获得Java 5 JDK ;-)
我正在尝试使用JAXB将xml文件解组为对象,但遇到了一些困难.实际项目在xml文件中有几千行,所以我在较小的范围内重现了错误,如下所示:
XML文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<catalogue title="some catalogue title"
publisher="some publishing house"
xmlns="x-schema:TamsDataSchema.xml"/>
Run Code Online (Sandbox Code Playgroud)
用于生成JAXB类的XSD文件
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="catalogue" type="catalogueType"/>
<xsd:complexType name="catalogueType">
<xsd:sequence>
<xsd:element ref="journal" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="title" type="xsd:string"/>
<xsd:attribute name="publisher" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)
代码段1:
final JAXBContext context = JAXBContext.newInstance(CatalogueType.class);
um = context.createUnmarshaller();
CatalogueType ct = (CatalogueType)um.unmarshal(new File("file output address"));
Run Code Online (Sandbox Code Playgroud)
哪个引发错误:
javax.xml.bind.UnmarshalException: unexpected element (uri:"x-schema:TamsDataSchema.xml", local:"catalogue"). Expected elements are <{}catalogue>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown …
Run Code Online (Sandbox Code Playgroud) 我刚刚注意到JDK 6设置默认TimeZone的方法与JDK5不同.
以前,新的默认值将存储在线程局部变量中.使用JDK6(我刚刚查看了1.6.0.18),实现已经改变,因此如果用户可以写入"user.timezone"属性,或者如果没有安装SecurityManager,则时区会在整个VM范围内发生变化!否则会发生线程局部更改.
我错了吗?这似乎是一个相当大的变化,我在网上找不到任何关于它的东西.
这是JDK6代码:
private static boolean hasPermission() {
boolean hasPermission = true;
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
try {
sm.checkPermission(new PropertyPermission("user.timezone", "write"));
} catch (SecurityException e) {
hasPermission = false;
}
}
return hasPermission;
}
/**
* Sets the <code>TimeZone</code> that is
* returned by the <code>getDefault</code> method. If <code>zone</code>
* is null, reset the default to the value it had originally when the
* VM first started.
* @param zone the new default …
Run Code Online (Sandbox Code Playgroud) 我正在测试RESTful服务,当我执行时我得到异常,虽然我的类路径(WEB-INF/lib)中有以下jar,但我没有使用Maven,我的JDK版本是1.5.有关此问题的其他问题无助于解决问题.
代码段
@GET
@Produces("application/json")
//@Produces({MediaType.APPLICATION_JSON}) tried this, didn't work either
public List<Emp> getEmployees() {
List<Emp> empList = myDAO.getAllEmployees();
log.info("size " + empList.size());
return empList;
}
@XmlRootElement
public class Emp {
......
Run Code Online (Sandbox Code Playgroud)
web.xml中
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>test.employees</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
罐子列表
jersey-server-1.2.jar
jersey-core-1.2.jar
jsr311-api-1.1.jar
asm-3.1.jar
jaxb-api-2.0.jar
jaxb-impl-2.0.jar
jackson-xc-1.2.0.jar
jackson-jaxrs-1.2.0.jar
jackson-mapper-asl-1.2.0.jar
jackson-core-asl-1.2.0.jar
jettison-1.2.jar
jersey-client-1.2.jar
jersey-servlet-1.10.jar
jersey-json-1.8.jar
Run Code Online (Sandbox Code Playgroud)
异常堆栈
SEVERE: A message body writer for Java class java.util.ArrayList,
and Java type …
Run Code Online (Sandbox Code Playgroud) 我正在使用JDK 1.4和1.5运行此代码并获得不同的结果.为什么会这样?
String str = "";
int test = 3;
str = String.valueOf(test);
System.out.println("str[" + str + "]\nequals result[" + (str == "3") + "]");
if (str == "3") {
System.out.println("if");
} else {
System.out.println("else");
}
Run Code Online (Sandbox Code Playgroud)
输出:
在jdk 1.4
str[3]
equals result[true]
if
Run Code Online (Sandbox Code Playgroud)在jdk 1.5
str[3]
equals result[false]
else
Run Code Online (Sandbox Code Playgroud)