java中2个数组的联合?

我的代码 class Union { //Search Function static boolean search(int A[], int i) { for (int k = 0; k < A.length; k++) { if (A[k] == i) { return true; } } return false; } //union static void union(int A[][], int B[][]) { int i = 0; int count = 0; int C[] = new […]

使用swing在java中为JButton创建热键

我使用以下代码使用swing为java表单创建热键。 如果我按ALT + N,ALT + R,ALT + 1,ALT + 2,光标移动到正确的文本字段,我在相应的文本字段中输入值。 它工作正常。 我的问题是,我保存并以这种forms退出JButton如果。 我按CTRL + S表示同时选择保存按钮如果按CTRL + X表示将选择退出按钮。 如何为JButton创建助记符? 如何使用以下代码执行CTRL + S,CTRL + X? 提前致谢。 package hotkeys; import java.awt.event.*; import javax.swing.*; import java.net.*; public class hotkey extends JFrame { public static void main(String arg[]) { JLabel Name = new JLabel(“Name”); JTextField tf1 = new JTextField(20); Name.setLabelFor(tf1); […]

如何在java中使用枚举的线程安全性?

如何在java中使用枚举的线程安全性? 我正在使用枚举实现一个Singleton(根据Bloch的Effective Java),我应该担心我的单例枚举的线程安全吗? 有没有办法certificate或certificate它是线程安全的? // Enum singleton – the preferred approach public enum Elvis { INSTANCE; public void leaveTheBuilding() { … } } 谢谢

使用Streams API对Collection中的n个随机不同元素执行操作

我正在尝试使用Java 8中的Streams API从集合中检索n个唯一的随机元素以进行进一步处理,但是没有太多或任何运气。 更准确地说,我想要这样的东西: Set subList = new HashSet(); Queue collection = new PriorityQueue(); collection.addAll(Arrays.asList(1,2,3,4,5,6,7,8,9)); Random random = new Random(); int n = 4; while (subList.size() v.doSomethingFancy()); 我想尽可能高效地做到这一点。 可以这样做吗? 编辑:我的第二次尝试 – 虽然不是我的目标: List sublist = new ArrayList(collection); Collections.shuffle(sublist); sublist.stream().limit(n).forEach(v -> v.doSomethingFancy()); 编辑:第三次尝试(灵感来自Holger ),如果coll.size()很大且n很小,这将消除很多shuffle的开销: int n = // unique element count List sublist = new ArrayList(collection); […]

我可以用MySQL Connector / J执行多个以分号分隔的查询吗?

我的mysql db的jdbc驱动程序是版本5.1.25。 我想像这样执行sql查询: statement.execute(“select fullName from user where user_id=1; select fullName from user where user_id=2”); 我总是收到exception: Exception in thread “main” com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘select fullName from user where user_id=2’ at line 1 […]

运行javafx程序时发生InvocationTargetException

所以这在javafx的例子中起作用,当时我的电脑有jdk 1.7.0,所以这可能是java8中FX的新版本; 我得到了一个很好的堆栈跟踪 jfx-project-run: Executing E:\workspace\PathFinderApp\dist\run1095471771\PathFinderApp.jar using platform C:\Program Files\Java\jdk1.8.0\jre/bin/java Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894) at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56) at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158) at […]

你如何在用java编写的selenium webdriver程序中使用firefox插件?

我试图运行一个selenium脚本,点击我的工具栏中的一个firefox插件。 是否有可能做到这一点?

Android Showcase查看如何使用?

很好的展示视图 我用这个: https://github.com/amlcurran/ShowcaseView 导入文件后,它会出错。 这是我的错误和improted .jar文件 错误说 在java中 R无法解析为变量 很有型 错误:检索项目的父项时出错:找不到与给定名称“Theme.Sherlock.Light”匹配的资源。 再次,风格 错误:错误:找不到与给定名称匹配的资源:attr’android:fontFamily’。 还有任何教程在我的项目中使用Showcase视图。 我找不到,我从github项目中没有理解。 不清楚。

从Java中的相对URL构建绝对URL

我无法从相对URL构建绝对URL而无需诉诸String hackery … 特定 http://localhost:8080/myWebApp/someServlet 方法内部: public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } 什么是最“正确”的建设方式: http://localhost:8080/myWebApp/someImage.jpg (注意,必须是绝对的,而不是相对的) 目前,我是通过构建字符串来实现的,但必须有更好的方法。 我查看了新URI / URL的各种组合,我最终得到了 http://localhost:8080/someImage.jpg 非常感谢

如何使用Comparator对ArrayList进行排序?

我有一个实现静态方法的类学生 public static Comparator getCompByName() 返回Student的新比较器对象,通过属性“name”比较2个Students对象。 我现在需要使用我的函数getCompByName()通过’name’对学生ArrayList进行排序来测试它。 这是我的Student课程中的Comparator方法。 public static Comparator getCompByName() { Comparator comp = new Comparator(){ @Override public int compare(Student s1, Student s2) { return s1.name.compareTo(s2.name); } }; return comp; } 而主要我需要测试的地方 public static void main(String[] args) { // TODO code application logic here //——–Student Class Test——————————————- ArrayList students = new ArrayList(); Student […]