如何为模式编译转义方括号

我用逗号分隔正则表达式列表: .{8},[0-9],[^0-9A-Za-z ],[AZ],[az] 我对逗号进行了分割。 现在我正在尝试将此正则表达式与生成的密码进行匹配。 问题是Pattern.compile不喜欢没有转义的方括号。 有些人可以给我一个简单的函数,它接受如下字符串: [0-9]并返回转义字符串\[0-9\] 。

将静态库与JNI链接

Java 8之前的Java版本要求本机代码位于共享库中,但我已经阅读了Java 8,可以将静态链接库与JNI一起使用。 我搜索了一些例子,却找不到任何例子。 如何将JNI库静态链接到我的java应用程序?

将java中的字符串拆分为相等长度的子字符串,同时保持字边界

如何在保持字边界的同时将字符串拆分为最大字符长度的相等部分? 比如说,如果我想将一个字符串“hello world”拆分成最多7个字符的相等子串,它应该返回给我 “hello ” 和 “world” 但我目前的实施回归 “hello w” 和 “orld ” 我使用以下代码从Split字符串中取代Java中相等长度的子字符串,将输入字符串拆分为相等的部分 public static List splitEqually(String text, int size) { // Give the list the right capacity to start with. You could use an array // instead if you wanted. List ret = new ArrayList((text.length() + size – 1) / size); for (int […]

如何将手机号码分成国家代码,区号和本地号码?

如何将手机号码分成国家代码,区号和本地号码? 分裂后为+919567123456 国家代码= 91 区号= 9567 当地号码= 123456

validationSpring中的对象列表

我有以下控制器方法: @RequestMapping(value=”/map/update”, method=RequestMethod.POST, produces = “application/json; charset=utf-8”) @ResponseBody public ResponseEntityWrapper updateMapTheme( HttpServletRequest request, @RequestBody @Valid List categories, HttpServletResponse response ) throws ResourceNotFoundException, AuthorizationException { … } CompanyTag以这种方式定义: public class CompanyTag { @StringUUIDValidation String key; String value; String color; String icon; Icon iconObj; public String getKey() { return key; } public void setKey(String key) { this.key […]

java Lambda与Anonymous类之间的执行时间差异很大

我对同一个匿名类创建java8 lambda实例的性能感到好奇。 (在win32 java build 1.8.0-ea-b106上执行测量)。 我创建了一个非常简单的示例,并测量java是否在创建lambda表达式时提出了一些new运算符的优化: static final int MEASURES = 1000000; static interface ICallback{ void payload(int[] a); } /** * force creation of anonymous class many times */ static void measureAnonymousClass(){ final int arr[] = {0}; for(int i = 0; i < MEASURES; ++i){ ICallback clb = new ICallback() { @Override public void […]

在Selenium WebDriver中实现InternetExplorerDriver期间发生NoSuchElementException

目前,我正在研究WebDriver来调用IE浏览器来运行测试。 但是当我尝试运行下面的简单示例时,我收到了NoSuchElementException 。 但是,如果我使用Chrome驱动程序或Firefox驱动程序,代码工作正常。 任何想法或想法将不胜感激。 Jar: selenium-server-standalone-2.5.0.jar 码: import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; public static void main(String[] args) throws InterruptedException { DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); WebDriver driver = new InternetExplorerDriver(ieCapabilities); driver.get(“www.google.com”); driver.findElement(By.name(“q”)); } 错误信息: Exception in thread “main” org.openqa.selenium.NoSuchElementException: Unable to find element with name == q (WARNING: The server […]

如何用Joda-Time计算两年中两个日期之间的差异……等等

import java.text.SimpleDateFormat; import java.util.Date; import org.joda.time.*; public class Test { public static void main(String[] args) { String dateStart = “01/01/2000 05:30”; String dateStop = “02/2/2001 06:31”; SimpleDateFormat format = new SimpleDateFormat(“MM/dd/yyyy HH:mm”); Date d1 = null; Date d2 = null; try { d1 = format.parse(dateStart); d2 = format.parse(dateStop); DateTime dt1 = new DateTime(d1); DateTime dt2 […]

为什么indexOf无法找到对象?

我创建了一个整数列表,并尝试返回特定值的索引。 数组是3,8,2,5,1,4,7,6,我想返回indexOf(3),它应该是0。 在导入java.util之后,我在Eclipse Java Scrapbook中尝试了以下内容。*: int[] A = {3,8,2,5,1,4,7,9}; Arrays.asList(A).indexOf(3) 我也尝试过: int[] A = {3,8,2,5,1,4,7,6}; ArrayList l = new ArrayList(Arrays.asList(A)); l.indexOf(3) 两者都返回-1。 为什么? 如何让它按预期工作?

更好的做法是重新实例化List或调用clear()

使用Java(1.6)最好是在List上调用clear()方法还是只重新实例化引用? 我有一个ArrayList,它填充了未知数量的对象,并定期“刷新” – 处理对象并清除列表。 刷新后,List再次填满。 冲洗是在随机时间发生的。 列表中的数字可能很小(对象的10个)或大的(数百万个对象)。 那么“flush”调用clear()或new ArrayList()会更好吗? 是否值得担心这类问题,还是我应该让VM担心呢? 我怎样才能看看Java的内存占用为我自己做这类事情? 任何帮助非常感谢。