我想在java中编写一个泛型方法,如下所示:
public <T extends Number & Comparable<T>> void test(T[] a){
T b=a[0];
if(a[0]>0){
a[0]*=a[0];
b+=a[1];
}
}
Run Code Online (Sandbox Code Playgroud)
再后来,我可以提供任一Integer[]
或Double[]
或其他Number
亚型的方法.但我上面尝试的代码给了我错误.
请帮我.谢谢.
对此的解决方案可能非常简单,但我不确定我缺少什么.这就是我拥有的,PropertyPlaceholderConfigurer
不会取代的${...}
.
/* ---- org/company/springtest/Test.java: ---- */
package org.company.springtest;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main( String... args ) {
Resource res = new FileSystemResource("conf/xml/context2.xml");
XmlBeanFactory beanFactory = new XmlBeanFactory(res);
TestApp app = (TestApp) beanFactory.getBean("testApp");
app.print();
}
}
/* ---- org/company/springtest/TestApp.java: ---- */
package org.company.springtest;
import org.springframework.beans.factory.annotation.Required;
public class TestApp {
private String m_message;
public void setMessage( String message ) {
m_message = message;
}
public void print() {
System.out.println(m_message); …
Run Code Online (Sandbox Code Playgroud) 我试图解决以下问题.有两个大小为n的阵列A和大小为n + 1的B. A和B具有相同的所有元素.B有一个额外的元素.找到元素.
我的逻辑是将数组转换为列表并检查B中的每个元素是否存在于A中.
但是当我使用原始数组时,我的逻辑不起作用.如果我正在使用
Integer [] a ={1,4,2,3,6,5};
Integer [] b = {2,4,1,3,5,6,7};
Run Code Online (Sandbox Code Playgroud)
我的代码运行正常.
public static void main(String [] args)
{
int [] a ={1,4,2,3,6,5};
int [] b = {2,4,1,3,5,6,7};
List<Integer> l1 = new ArrayList(Arrays.asList(a));
List<Integer> l2 = new ArrayList(Arrays.asList(b));
for(Integer i :l2)
{
if(!l1.contains(i))
{
System.out.println(i);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的逻辑也是O(n + 1).还有更好的算法吗?
谢谢
我需要一个Timer
基本上每t秒做一些事情.但我希望能够修改计时器重复任务的计时器周期.我写了这样的东西:
public Bot() {
timer = new Timer();
timer.schedule(new Task(), 1000, moveTime = 1000);
}
public class Task extends TimerTask {
@Override
public void run() {
System.out.println("Time Passed from last repeat:" + movetime)
moveTime += 1000;
}
Run Code Online (Sandbox Code Playgroud)
因此,在1000ms延迟后,定时器启动并重复每moveTime
ms.问题是即使我增加movetime
1000,计时器总是以初始延迟(1000)运行,但movetime
每次定时器调用时增加值(2000,3000,4000等)run()
.
我错过了什么或者我有什么选择,每隔't'秒重复一次任务,'t'是可变的?
谢谢.
我一直在阅读volatile
和阅读synchronized
Java,但一直在困惑中摸不着头脑.我希望有人可以帮我解决问题
private HashMap<String,int> map = new HashMap<String,int>();
Run Code Online (Sandbox Code Playgroud)
在我的线程中
if (map.get("value") == null)
{
map.put("value",0);
}
map.put("value",map.get("value")+1);
Run Code Online (Sandbox Code Playgroud)
我的目标是让所有线程分享这个map
.如果我添加volatile
它似乎并没有解决我的问题(我输出并看到map
每次都被覆盖).然后我尝试使用ConcurrentHashMap
并volatile
在前面添加...这似乎也没有用.根据我的阅读,volatile
我的理解是它应该"锁定"对写入map
时的访问权限map
,然后在map
写入锁定时完成.
所以...然后我尝试添加 static
private static ConcurrentHashMap<String,int> map = new ConcurrentHashMap<String,int>();
Run Code Online (Sandbox Code Playgroud)
这看起来很完美......但是......我一直在读,static
由于"争用"(我不太明白),使用不是正确的方法
提前致谢
我试图用Java编写递归的Ackermann函数.但我觉得我在某个地方出错了!任何人都可以看看,检查并指出我正确的方向纠正我的代码?谢谢!
我对代码的问题是,在我写完之后,我想,如果n == 0和m == 0,那么这个区域没有?这会属于if(m == 0)还是需要它自己的if语句?
我的以下解决方案是否正确 如果我以不同的顺序给它相同的数字,它给出了不同的结果,我不确定这是否是这种情况.
public static int ackermann(int m, int n) {
if (m == 0) {
return n + 1;
} else if ((m > 0) && (n == 0)) {
return ackermann(m-1, n);
} else if ((m > 0) && (n > 0)) {
return ackermann(m-1, ackermann(m,n-1));
} else {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我想了一下,我想我错了.如果你无法弄清楚我做了什么我给了每个if语句相反,因为我认为如果以不同的方式给出m和n值,则下面的代码将起作用.它显然没有,但有人可以尝试解释我哪里出错了?
public static int ackermann(int m, int n) {
if (m == 0) {
return n + 1; …
Run Code Online (Sandbox Code Playgroud) 看看这个页面:http://www14.software.ibm.com/webapp/download/search.jsp? go = y&rs = ifxjdbc
我看到3.50.JC9比3.70JC3DE更新,这令人困惑.
我测试了它们都连接到IDS 11.5和10.0,两者似乎都运行正常.
有谁知道我应该在哪种条件下使用3.50或3.70 JDBC驱动程序?
为什么以下java代码没有生成编译器警告,说"从SuperClass到SomeBaseClass的不安全强制转换"?
public abstract SuperClass
{
static SuperClass create()
{
return new AnotherBaseClass();
}
private static class SomeBaseClass extends SuperClass
{
void print()
{
System.out.println("Hello World");
}
}
private static class AnotherBaseClass extends SuperClass
{
}
public static void main(String[] args)
{
SomeBaseClass actuallyAnotherClass = (SomeBaseClass)SuperClass.create();
actuallyAnotherClass.print();
}
}
Run Code Online (Sandbox Code Playgroud)
我在Windows机器上使用了jdk1.6.0_25/bin/javac.Eclipse Helios也没有对此发出警告.
相反,它会导致运行时异常:
线程"main"中的异常java.lang.ClassCastException:SuperClass $ AnotherBaseClass无法强制转换为SuperClass $ SomeBaseClass
我试图在一些速度模板中访问HttpServletRequest但从未成功.我已经尝试过以下语法风格
当前URL:$ req.get("attributes").get("CURRENT_URL"))结果>当前URL:$ req.get("attributes").get("CURRENT_URL"))
当前URL:$ request.get("attributes").get("CURRENT_URL"))结果>当前URL:$ request.get("attributes").get("CURRENT_URL"))
当前URL:$ request.get("attributes").get("CURRENT_URL"))结果>当前URL:$ request.get("attributes").get("CURRENT_URL"))
当前网址:$ {request.get("attributes").get("CURRENT_URL"))}结果>当前网址:$ {request.get("attributes").get("CURRENT_URL"))}
注意:Web.xml看起来像
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- Define Velocity template compiler -->
<servlet>
<servlet-name>velocity</servlet-name>
<servlet-class>
org.apache.velocity.tools.view.servlet.VelocityViewServlet
</servlet-class>
<!--
Unless you plan to put your toolbox.xml and velocity.properties
under different folders or give them different names, then these
two init-params are unnecessary as of VelocityTools 1.3. The
VelocityViewServlet will automatically look for these files in …
Run Code Online (Sandbox Code Playgroud) 我在JAVA的同一个包中有两个类.
我有一个构造函数的一个类,我尝试创建自己:
public class ZooException extends Exception {
public ZooException(String error) {
super(error);
}
}
Run Code Online (Sandbox Code Playgroud)
另一个类需要在某一点调用此异常:
public class Zoo<T extends Animal>
Zoo(int capacity) {
if (capacity <= 1) {
throw new ZooException("Zoo capacity must be larger than zero");
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里注意到两件事
关于我可以做什么来解决Zoo类中的这个错误的想法?先感谢您!