小编nam*_*olk的帖子

Java Regex中的零长度匹配

我的代码:

Pattern pattern = Pattern.compile("a?");
Matcher matcher = pattern.matcher("ababa");
while(matcher.find()){
   System.out.println(matcher.start()+"["+matcher.group()+"]"+matcher.end());
}
Run Code Online (Sandbox Code Playgroud)

输出:

0[a]1
1[]1
2[a]3
3[]3
4[a]5
5[]5
Run Code Online (Sandbox Code Playgroud)

我知道的 :

  • "一个?" 代表字符'a'的零或一次出现.

Java API说:

  • matcher.start()返回上一个匹配的起始索引.
  • matcher.end()返回最后一个字符匹配后的偏移量.
  • matcher.group()返回上一个匹配项匹配的输入子序列.对于具有输入序列s的匹配器m,表达式m.group()和s.substring(m.start(),m.end())是等价的.对于某些模式,例如a*,匹配空字符串.当模式成功匹配输入中的空字符串时,此方法将返回空字符串.

我想知道的:

  1. 在哪种情况下,正则表达式引擎遇到给定字符的零次出现 - 这里是字符'a'.
  2. 在这种情况下,值实际上由匹配器中的start(),end()和group()方法返回.我已经提到过java API所说的内容.但是,就上述实际情况而言,我不太清楚.

java regex

8
推荐指数
1
解决办法
3120
查看次数

将对象添加到"?extends"类型的通用集合中

public void addAllAnimals(ArrayList<? extends Animal> animalLikeList){

 // I need to add animal objects (eg Dog, Cat that extends Animal) to animalLikeList.

}
Run Code Online (Sandbox Code Playgroud)

我知道它不允许直接添加,因为? extends Animal代表未知的子类型Animal.

我的问题是:是不是有什么办法(间接的方式),以addaddAll一个Animal或亚型Animal对象的animalLikeList

java generics collections

7
推荐指数
1
解决办法
4311
查看次数

如何评论所有行包含eclipse中的一些文本

我正在用eclipse来煮java.现在,当我想要评论包含特定文本/变量的所有行时,我有几次遇到这个问题.有什么办法吗?

java eclipse

6
推荐指数
1
解决办法
381
查看次数

如何在Maven中执行多个Ant目标

是的,是的,这是XY类型的问题。(我想在不丢失任何信息的情况下更好地展示它)

在我的项目中,当我尝试在maven中执行ANT任务时,它给了我与该示例相同的错误。我从这里举了这个例子。

我尝试使用来执行多个ant目标maven-antrun-plugin。但是,它总是只执行最下面的目标(当我不提及依赖服装时)。当我使用它时,它给出以下异常:

Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (default) on project jibx-mvn-demo: An Ant BuildException has occured: Target "compile" does not exist in the project "maven-antrun-". It is used from target "myDAO". -> [Help 1]
Run Code Online (Sandbox Code Playgroud)

pom.xml

    <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>install</phase>
                    <configuration>
                        <target name="clean">
                            <echo>Clean up my working directory to be nice and sparkly</echo>
                        </target>
                        <target name="initDAO">
                            <echo>Initialize stuff for my DAO build</echo>
                            <echo>Maybe setup some properties?</echo>
                        </target>
                        <target name="makedir" depends="initDAO">
                            <echo>I need my directories …
Run Code Online (Sandbox Code Playgroud)

java eclipse ant maven

6
推荐指数
2
解决办法
5363
查看次数

设置HTTPS连接的https.protocols系统属性的问题

我有一个Java实现,各种客户端应用程序使用它来连接到第三方系统。这些第三方系统通过http / https支持不同的协议。在这种情况下,所有客户端应用程序都托管在Java实现所在的同一服务器上。因此,在这种情况下,各种客户端应用程序会为系统属性设置各种https协议(例如:System.setProperty("https.protocols", "SSLv3")System.setProperty("https.protocols", "TLS")当它们使用此协议连接到那些第三方系统时)。

在这里,系统属性在该环境中的所有应用程序之间共享。因此,修改System属性会导致许多问题。所以,我想知道

  1. 有没有办法在不使用系统属性的情况下完成此任务?
  2. 有没有办法设置所有可能的https.protocols,使其支持与第三方系统建立的任何http或https连接,从而支持各种协议?

blogs.oracle.com中提到的每个JDK版本都支持的协议和算法: 在此处输入图片说明

代码:

String responseStr = null;

System.setProperty("https.protocols",http-protocol); // This is set by the client applications. Previously, there was one by one (eg : "SSLv3". Then I changed it to "TLSv1.2,TLSv1.1,TLSv1,SSLv3" assuming it will enable all) 

byteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(requestStr.getBytes());

URL mUrl = new URL(proxy_url);
HttpURLConnection con = (HttpURLConnection) mUrl.openConnection(); // It works fine for the HttpURLConnection when there's no (s)

con.setRequestMethod("POST");
con.setDoOutput(true);
con.setUseCaches(false);
con.setDoInput(true);

con.setRequestProperty("user-agent","Mozilla(MSIE)"); …
Run Code Online (Sandbox Code Playgroud)

java ssl https

6
推荐指数
1
解决办法
2万
查看次数

使用try-catch over条件来安全地设置值,并在java中以最小的性能影响

在这里,我的主要目标是安全地设置值,而不会产生性能(速度,内存,CPU等)影响.

我有一个愚蠢的选择(坏的风格)也在下面提到.那么,最好的方法是什么?选项1?选项2?还是另一个?

选项1 :

if(
    animalData!=null && 
    animalData.getBreedData()!=null && 
    dogx.getBreed() != null && dogx.getBreed().getBreedCode() != null && 
    animalData.getBreedData().get(dogx.getBreed().getBreedCode()) != null
){
    dogx.getBreed().setBreedId(animalData.getBreedData().get(dogx.getBreed().getBreedCode()));
}
Run Code Online (Sandbox Code Playgroud)

选项2:

try{dogx.getBreed().setBreedId(animalData.getBreedData().get(dogx.getBreed().getBreedCode()));}catch(Exception e){}
Run Code Online (Sandbox Code Playgroud)

注意:这段代码是一个循环,有数千次迭代.

java performance if-statement exception-handling

6
推荐指数
1
解决办法
98
查看次数

适用于java的信用卡验证器

我需要进行信用卡号码验证.

当我用Google搜索时,我找到了org.apache.commons.validator.CreditCardValidator.但似乎它无法正常工作.当我传递一个非数字字符时,它也是真实的.

Apache CreditCardValidator的代码:

String ccNumber = "378282246310005";
CreditCardValidator creditCardValidator = new CreditCardValidator();
if(!creditCardValidator.isValid(ccNumber)) throw new Exception("Credit Card Number is not a valid one!");
Run Code Online (Sandbox Code Playgroud)

然后,我根据卡片类型和卡号(使用luhn算法)编写了以下方法来验证信用卡号码.

CardType验证器(如果卡类型无效,则为null)

public String getCCType(String ccNumber){

        String visaRegex        = "^4[0-9]{12}(?:[0-9]{3})?$";
        String masterRegex      = "^5[1-5][0-9]{14}$";
        String amexRegex        = "^3[47][0-9]{13}$";
        String dinersClubrRegex = "^3(?:0[0-5]|[68][0-9])[0-9]{11}$";
        String discoverRegex    = "^6(?:011|5[0-9]{2})[0-9]{12}$";
        String jcbRegex         = "^(?:2131|1800|35\\d{3})\\d{11}$";
        String commonRegex      = "^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$";

        try {
            ccNumber = ccNumber.replaceAll("\\D", "");
            return (ccNumber.matches(visaRegex) ? "VISA" : ccNumber.matches(masterRegex) ? "MASTER" :ccNumber.matches(amexRegex) ? "AMEX" :ccNumber.matches(dinersClubrRegex) ? …
Run Code Online (Sandbox Code Playgroud)

java validation credit-card

5
推荐指数
1
解决办法
8536
查看次数

为什么一旦线程启动,它就永远不能在java中再次启动

我知道"一旦线程启动,就永远无法重新启动".
但我想知道为什么?
如果允许在其他时间再次启动,那有什么不对?
为什么,唯一可以启动线程的时候是它处于NEW状态?为什么至少在DEAD之后也不能呢?

public class ThreadDemo {

    public static void main(String[] args) {

        Thread thread = new Thread(new MyRunnable());
        thread.start();
        thread.start(); // java.lang.IllegalThreadStateException

    }

}

class MyRunnable implements Runnable{

    @Override
    public void run() {

        System.out.println("run().Thread.currentThread().getName() : " + Thread.currentThread().getName());

    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我已经完成了这些帖子.但我的问题更加具体和具有描述性.

在这里,请注意我想知道这主要是为了理解线程内部功能以及GC等相关方面如何与线程状态一起工作.

java multithreading

5
推荐指数
1
解决办法
241
查看次数

为什么Ternary运算符不在java中的方法参数内工作

我在开发过程中得到了这一点.

为什么Ternary运算符不在方法参数内工作?这里显然InputStream或(否则)String.

class A{

    public  static boolean opAlpha(InputStream inputStream) {

        // Do something

        return true;

    }

    public static boolean opAlpha(String arg) {

        // Do something else

        return true;
    }

    public static void main(String[] args) throws Exception {

        boolean useIsr = true;

        InputStream inputStream = null;
        String arg = null;

//      boolean isTrue = useIsr ? A.opAlpha(inputStream): A.opAlpha(arg); // This is OK.
        boolean isTrue =  A.opAlpha(useIsr ? inputStream : arg); // This is not. (Error : The …
Run Code Online (Sandbox Code Playgroud)

java ternary-operator

5
推荐指数
2
解决办法
845
查看次数

在CouchDB中设置显式响应Content-Type

我正在寻找一种方法来设置Contet-Typefor couchdb响应.

我有POST一个JSON使用Content-TypeAccept请求标题为application/json.但是当我调用GETfor该文档时,它会返回JSON响应Content-Type text/plain; charset=utf-8.我所期待的就是得到application/jsonContent-Type.

我怎样才能做到这一点?

POST

  • URL:http:// localhost:5984/hotel_staticdata
  • 请求机构: {"_id":"test-hotel-stat-data","content":[{"hotelcode": "AMB3_LON","hotelname": "Ambassadors Bloomsbury"},{"hotelcode": "ALE1_LON","hotelname": "Alexandra"},{"hotelcode": "ALE1_LON","hotelname": "Alexandra"}]}
  • 请求标题:接受:application/json Content-Type:application/json

得到

  • URL: http:// localhost:5984/hotel_staticdata/test-hotel-stat-data
  • 回应机构: {"_id":"test-hotel-stat-data","_rev":"3-6c6e26cd5690794886a8dc65308bf078","content":[{"hotelcode":"AMB3_LON","hotelname":"Ambassadors Bloomsbury"},{"hotelcode":"ALE1_LON","hotelname":"Alexandra"},{"hotelcode":"ALE1_LON","hotelname":"Alexandra"}]}
  • 响应标题:内容长度:249内容类型:text/plain; 字符集= utf-8的

而不是这个,我尝试PUT然后GET但结果是相同的.

  • CouchDB版本:1.6.1

json couchdb

5
推荐指数
1
解决办法
186
查看次数