我的代码:
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)
我知道的 :
Java API说:
我想知道的:
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.
我的问题是:是不是有什么办法(间接的方式),以add或addAll一个Animal或亚型Animal对象的animalLikeList?
我正在用eclipse来煮java.现在,当我想要评论包含特定文本/变量的所有行时,我有几次遇到这个问题.有什么办法吗?
是的,是的,这是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实现,各种客户端应用程序使用它来连接到第三方系统。这些第三方系统通过http / https支持不同的协议。在这种情况下,所有客户端应用程序都托管在Java实现所在的同一服务器上。因此,在这种情况下,各种客户端应用程序会为系统属性设置各种https协议(例如:System.setProperty("https.protocols", "SSLv3"),System.setProperty("https.protocols", "TLS")当它们使用此协议连接到那些第三方系统时)。
在这里,系统属性在该环境中的所有应用程序之间共享。因此,修改System属性会导致许多问题。所以,我想知道
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) 在这里,我的主要目标是安全地设置值,而不会产生性能(速度,内存,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)
注意:这段代码是一个循环,有数千次迭代.
我需要进行信用卡号码验证.
当我用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) 我知道"一旦线程启动,就永远无法重新启动".
但我想知道为什么?
如果允许在其他时间再次启动,那有什么不对?
为什么,唯一可以启动线程的时候是它处于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等相关方面如何与线程状态一起工作.
我在开发过程中得到了这一点.
为什么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) 我正在寻找一种方法来设置Contet-Typefor couchdb响应.
我有POST一个JSON使用Content-Type和Accept请求标题为application/json.但是当我调用GETfor该文档时,它会返回JSON响应Content-Type text/plain; charset=utf-8.我所期待的就是得到application/json它Content-Type.
我怎样才能做到这一点?
POST
{"_id":"test-hotel-stat-data","content":[{"hotelcode": "AMB3_LON","hotelname": "Ambassadors Bloomsbury"},{"hotelcode": "ALE1_LON","hotelname": "Alexandra"},{"hotelcode": "ALE1_LON","hotelname": "Alexandra"}]}得到
{"_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"}]}而不是这个,我尝试PUT然后GET但结果是相同的.
java ×9
eclipse ×2
ant ×1
collections ×1
couchdb ×1
credit-card ×1
generics ×1
https ×1
if-statement ×1
json ×1
maven ×1
performance ×1
regex ×1
ssl ×1
validation ×1