我读到HashMap具有以下实现:
main array
?
[Entry] ? Entry ? Entry ? linked-list implementation
[Entry]
[Entry] ? Entry
[Entry]
[null ]
Run Code Online (Sandbox Code Playgroud)
因此,它有一个Entry对象数组.
问题:
我想知道如果相同的hashCode但不同的对象,这个数组的索引如何存储多个Entry对象.
这与LinkedHashMap
实施有何不同?它是map的双链表实现,但它是否像上面那样维护一个数组,它如何存储指向下一个和前一个元素的指针?
我有一个实现接口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();
这段代码会起作用,我将如何设置单词?
这是代码:
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."他不在那里.为什么会这样?不确定我在这里做错了什么!
我有一个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(?);
......
for(int i=0; i<10;i++){
int j=0;
}
Run Code Online (Sandbox Code Playgroud)
是ja块变量还是局部变量?我看到j的范围只有for循环结束
它可能是一个非常简单的但它仍然让我感到困惑!
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会改变?虽然不是原始的!
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(),这是它何时进入超类构造函数!
标签未对齐中心布局的左侧.如果GridLayout
不存在,则它正确移动.有没有办法将JLabel移到最左边?
我试过setHorizontalAlignment和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) 尝试运行一个简单的基于 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) 什么时候是不安全且安全的?
安全(这么认为):来源 - [这里]
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 ×10
arrays ×1
casting ×1
constructor ×1
file-io ×1
grid-layout ×1
hashmap ×1
inheritance ×1
interface ×1
jlabel ×1
spring ×1
string ×1
swing ×1