小编nak*_*kul的帖子

这些集允许null.为什么我不能添加null元素?

我想知道为什么HashSet,LinkedHashSet和TreeSet实现不允许null元素?每当我尝试运行以下代码时,它会抛出一个空指针异常.

public static void main(String[] args) {

    HashSet<Integer> hashSet = new HashSet<Integer>();

    hashSet.add(2);
    hashSet.add(5);
    hashSet.add(1);
//  hashSet.add(null);  will throw null pointer 
    hashSet.add(999);
    hashSet.add(10);
    hashSet.add(10);
    hashSet.add(11);
    hashSet.add(9);
    hashSet.add(10);
    hashSet.add(000);
    hashSet.add(999);
    hashSet.add(0);

    Iterator<Integer> it = hashSet.iterator();
    while(it.hasNext()){
        int i = it.next();
        System.out.print(i+" ");
    }
    }
Run Code Online (Sandbox Code Playgroud)

请指导我.

java null hashset

25
推荐指数
4
解决办法
8万
查看次数

".+"和".+?"之间的区别

有人可以解释的差异之间.+.+?

我有字符串: "extend cup end table"

  1. 模式e.+d发现:extend cup end
  2. 模式e.+?d发现:extendend

我知道这+是一个或多个,?是一个或零.但我无法理解它是如何工作的.

regex

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

为什么我们必须覆盖Java中的equals()方法?

我对我们覆盖该.equals方法的原因感到困惑.

例如:

Test test1 = new Test(3);
Test test2 = new Test(3);

//The if comparison does the same thing that the overridden `.equals()` method does.
if(test1.equals(test2)){
    System.out.println("test1 and test2 are true in .equals()");
}

// Override .equals method.
public boolean equals(Object object) {
    if(object instanceof Test && ((Test)object).getValue() == this.t) {
        return true;
    } else {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我们必须覆盖该.equals()方法.

java overriding equals

18
推荐指数
3
解决办法
20万
查看次数

面对weblogic中的perm gen space错误

我是weblogic的新手.在我看到管理员控制台启动服务器并登录后,它会抛出异常.

Root cause of ServletException.
java.lang.OutOfMemoryError: PermGen space
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at weblogic.utils.classloaders.GenericClassLoader.defineClass(GenericClassLoader.java:335)
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:288)
Truncated. see log file for complete stacktrace
Run Code Online (Sandbox Code Playgroud)

我做了很多的谷歌和发现了一些解决方案来初始化JAVA_OPTIONS-XX:xmx等.我试图设置在这个startdomainenv.cmd文件,但没有运气.

请帮忙.任何指针都将受到高度赞赏.

谢谢.

java garbage-collection weblogic

12
推荐指数
2
解决办法
4万
查看次数

PrintWriter和FileWriter类之间的区别

try{

    File file = new File("write.txt");
    FileWriter writer = new FileWriter(file);

    PrintWriter printWriter = new PrintWriter(writer);
    printWriter.println("pqr");
    printWriter.println("jkl");
    printWriter.close();

    PrintWriter printWriter = new PrintWriter(file);
    printWriter.println("abc");
    printWriter.println("xyz");
    printWriter.close();
}
Run Code Online (Sandbox Code Playgroud)

我不明白这两种方式有什么区别.在哪种情况下我应该使用printWriter和fileWriter.

java file-io

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

使用 Arrays.sort() 方法对类型对象的数组进行排序

我知道如何使用 Arrays.sort() 方法按以下方式对对象数组进行排序。

Arrays.sort(array of primitive type);   
Arrays.sort(array of primitive type, from, to); 
Arrays.sort(array of an object type);   
Arrays.sort(array of an object type , from, to);    
Run Code Online (Sandbox Code Playgroud)

但我不知道以下两种方法。

Arrays.sort(array of an object type , comparator);  
Arrays.sort(array of an object type , from, to, comparator);    
Run Code Online (Sandbox Code Playgroud)

有人可以让我知道如何使用这些方法对类型对象的数组进行排序吗?我请求您添加代码或任何指向 .java 类的链接。我尝试搜索它但找不到它。

谢谢。

java sorting comparison

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