小编Mer*_*ary的帖子

LinkedHashMap的内部实现与HashMap实现有何不同?

我读到HashMap具有以下实现:

main array 
   ?
[Entry] ? Entry ? Entry      ? linked-list implementation
[Entry]
[Entry] ? Entry
[Entry]
[null ]
Run Code Online (Sandbox Code Playgroud)

因此,它有一个Entry对象数组.

问题:

  1. 我想知道如果相同的hashCode但不同的对象,这个数组的索引如何存储多个Entry对象.

  2. 这与LinkedHashMap实施有何不同?它是map的双链表实现,但它是否像上面那样维护一个数组,它如何存储指向下一个和前一个元素的指针?

java hashmap linkedhashmap

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

接口中没有setter方法

我有一个实现接口B的具体类A.

B ref = new A();
Run Code Online (Sandbox Code Playgroud)

代码:

public interface B{
  public abstract String[] getWords();
}

public class A implements B {
  private String[] words = new String[] {};
  public void setWords(String[] words){
    this.words = words;
  }
  public String[] getWords(){
    return this.words;
  }
 }
Run Code Online (Sandbox Code Playgroud)

在接口B中,我只有getter方法但没有setter方法,尽管A类有它.

所以当我这样做时:B ref = new A();这段代码会起作用,我将如何设置单词?

java inheritance interface

6
推荐指数
3
解决办法
1506
查看次数

使用BufferedReader时,第一个字符被截断

这是代码:

File file = new File("Hello.txt");
file.createNewFile();
FileWriter write = new FileWriter(file);
BufferedWriter bufWrite = new BufferedWriter(write);
bufWrite.write("HelloWorld");
bufWrite.flush();
bufWrite.close();

FileReader read = new FileReader(file);
BufferedReader bufRead = new BufferedReader(read);
while(bufRead.read()!=-1){
 System.out.println(bufRead.readLine());
}
bufRead.close();
Run Code Online (Sandbox Code Playgroud)

在这里,输出是elloWorld."他不在那里.为什么会这样?不确定我在这里做错了什么!

java file-io

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

将字符串数组值直接传递给setter方法

我有一个String数组,它有setter和getter方法.如何直接将值传递{"one", "two"}给setter方法,而不是先将值设置为变量然后传递参数?

String[] arr1 = {};
public String[] getArr1() {
    return arr1;
}

public void setArr1(String[] arr1) {
    this.arr1 = arr1;
}
Run Code Online (Sandbox Code Playgroud)

..期待像setArr1(?);......

java arrays string

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

在for循环中声明的变量的范围

for(int i=0; i<10;i++){
 int j=0;
}
Run Code Online (Sandbox Code Playgroud)

是ja块变量还是局部变量?我看到j的范围只有for循环结束

java

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

为什么一个arraylist在修改它的副本时会改变

它可能是一个非常简单的但它仍然让我感到困惑!

import java.util.ArrayList;

public class Sample {
    ArrayList<Integer> i = new ArrayList<>();
    ArrayList<Integer> j = new ArrayList<>();

    /**
     * @param args
     */
    public static void main(String[] args) {
        new Sample().go();
    }

    private void go() {

        i.add(1);
        i.add(2);
        i.add(3);

        j=i;

        i.remove(0);


        System.out.println(i + "asd" + j);
    }

}
Run Code Online (Sandbox Code Playgroud)

我试着打印它:

[2, 3]asd[2, 3]
Run Code Online (Sandbox Code Playgroud)

当我改变时,为什么j会改变?虽然不是原始的!

java

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

加载类时调用

public class SuperClass{
    public SuperClass(){
        System.out.println("Super class");
    }
}

public class SubClass extends SuperClass{
    int i;
    {
        i=10;
    }
    public SubClass(){
        System.out.println("Subclass");
    }
    public static void main(String[] args){
        SubClass sc = new SubClass();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在所有可能的地方进行了调试,我看到第一次创建实例时,它首先进入SuperClass构造函数并打印Super Class,然后它才会进入实例变量,然后是初始化块,最后是SubClass构造函数和打印子类.

但是,我已经阅读过某些内容,在子类构造函数中,默认情况下调用了super(),这是它何时进入超类构造函数!

java constructor

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

JLabel未与GridLayout正确对齐

标签未对齐中心布局的左侧.如果GridLayout不存在,则它正确移动.有没有办法将JLabel移到最左边?

我试过setHorizo​​ntalAlignment和setAlignmentX,两者都不起作用

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class asd {
    public static void main(String[] args){
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        JLabel lab = new JLabel("LABEL",SwingConstants.LEFT);
        //lab.setHorizontalAlignment(SwingConstants.CENTER);
        GridLayout grid = new GridLayout(3,3,2,1);
        JPanel yourGrid = new JPanel(grid);
        panel.add(lab);
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                JButton but = new JButton();
                yourGrid.add(but);
            }
        }
        panel.add(yourGrid);
        frame.getContentPane().add(BorderLayout.CENTER,panel);
        frame.setVisible(true);
        frame.pack();
    } …
Run Code Online (Sandbox Code Playgroud)

java swing jlabel grid-layout

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

java.lang.IllegalStateException:在 bean 定义中没有指定 bean 类

尝试运行一个简单的基于 spring 的程序。我正在使用 spring-framework-3.0.5。不知道是什么导致了这个错误!!

弹簧文件

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="user" name="com.spsam.UserPOJO">
        <property name="id" value="1351231"/>
        <property name="name" value="Ninam"/>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

示例类:

public class Sample1 {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        UserPOJO user = (UserPOJO) context.getBean("user");
        user.getUserDetails();
    }
}
Run Code Online (Sandbox Code Playgroud)

异常得到:

Dec 12, 2013 6:59:08 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@744607b5: startup date [Thu Dec 12 18:59:08 IST 2013]; root of context hierarchy
Dec 12, 2013 6:59:08 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions …
Run Code Online (Sandbox Code Playgroud)

java spring

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

安全和不安全的向下传播示例?

什么时候是不安全且安全的?

安全(这么认为):来源 - [这里]

class Useful {
  public void f() {}
  public void g() {}
}

class MoreUseful extends Useful {
  public void f() {}
  public void g() {}
  public void u() {}
  public void v() {}
  public void w() {}
}

public class RTTI {
  public static void main(String[] args) {
    Useful[] x = {
      new Useful(),
      new MoreUseful()
    };
    x[0].f();
    x[1].g();
    // Compile time: method not found in Useful:
    //! x[1].u();
    ((MoreUseful)x[1]).u(); // Downcast/RTTI
    ((MoreUseful)x[0]).u(); // Exception …
Run Code Online (Sandbox Code Playgroud)

java casting

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