Tag: java

什么是在Java中实现单例模式的有效方法?

什么是在Java中实现单例模式的有效方法?

重写到MVC后GUI不工作

我正在练习MVC风格的编程。 我在一个文件中有一个Mastermind游戏,工作正常(也许事实上,“检查”button在开始时是不可见的)。 http://paste.pocoo.org/show/226726/ 但是当我重写它来build模,查看,控制器文件 – 当我点击空Pin(应该更新,并重新绘制新颜色) – 注意到会发生。 有人可以在这里看到任何问题吗? 我试过把repaint()放在不同的地方,但根本不起作用:/ 主要: public class Main { public static void main(String[] args){ Model model = new Model(); View view = new View("Mastermind", 400, 590, model); Controller controller = new Controller(model, view); view.setVisible(true); } } 型号: import java.util.Random; public class Model{ static final int LINE = 5, SCORE […]

按属性sorting自定义对象的ArrayList

我读了关于使用比较器对ArrayLists进行sorting,但是在所有的例子中,人们使用了compareTo ,根据一些研究,这是一个Strings的方法。 我想通过其属性之一来sorting自定义对象的ArrayList:Date对象( getStartDay() )。 通常我通过item1.getStartDate().before(item2.getStartDate())比较他们item1.getStartDate().before(item2.getStartDate())所以我想知道我是否可以写这样的: public class CustomComparator { public boolean compare(Object object1, Object object2) { return object1.getStartDate().before(object2.getStartDate()); } } public class RandomName { … Collections.sort(Database.arrayList, new CustomComparator); … }

如何在Java中创build一个通用数组?

由于Javagenerics的实现,你不能有这样的代码: public class GenSet<E> { private E a[]; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation } } 我怎样才能实现这一点,同时保持types安全? 我在Java论坛上看到了这样一个解决scheme: import java.lang.reflect.Array; class Stack<T> { public Stack(Class<T> clazz, int capacity) { array = (T[])Array.newInstance(clazz, capacity); } private final T[] array; } 但我真的不知道发生了什么事。

将Java连接到MySQL数据库

你如何连接到Java中的MySQL数据库?

为什么我在Java中得到一个NoClassDefFoundError?

我运行我的Java应用程序时遇到NoClassDefFoundError 。 这通常是什么原因?

我怎样才能创build一个使用Maven的依赖关系的可执行JAR?

我想将我的项目打包在一个可执行的JAR中进行分发。 我如何使Maven将所有依赖JAR包装到我的JAR中?

为什么Spring MVC用404响应,并报告“在DispatcherServlet中没有find具有URI的HTTP请求的映射”?

我正在编写一个部署在Tomcat上的Spring MVC应用程序。 请参阅以下最小,完整且可validation的示例 : public class Application extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { }; } protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { SpringServletConfig.class }; } protected String[] getServletMappings() { return new String[] { "/*" }; } } SpringServletConfig在哪里 @Configuration @ComponentScan("com.example.controllers") @EnableWebMvc public class SpringServletConfig { @Bean public InternalResourceViewResolver resolver() […]

迭代集合,避免在循环中移除时出现ConcurrentModificationExceptionexception

我们都知道你不能这样做: for (Object i : l) { if (condition(i)) { l.remove(i); } } ConcurrentModificationException等…这显然有效,但并不总是。 这是一些特定的代码: public static void main(String[] args) { Collection<Integer> l = new ArrayList<Integer>(); for (int i=0; i < 10; ++i) { l.add(new Integer(4)); l.add(new Integer(5)); l.add(new Integer(6)); } for (Integer i : l) { if (i.intValue() == 5) { l.remove(i); } } […]

非静态variables不能从静态上下文中引用

我已经写了这个testing代码: class MyProgram { int count = 0; public static void main(String[] args) { System.out.println(count); } } 但它给出了以下错误: Main.java:6: error: non-static variable count cannot be referenced from a static context System.out.println(count); ^ 我如何让我的方法来识别我的类variables?