我試圖覆蓋已定義的變量。Java Array從屬性加載
這裏是我的代碼:
package com.diesal11;
import java.lang.reflect.Array;
public class Test{
private class List {
public String[] words;
public List(String[] array) {
this.words = array;
}
}
public List[] all;
public Test() {
this.all = new List[2];
String[] array = new String[2];
array[0] = "One";
array[1] = "Two";
this.all[0] = new List(array);
array[0] = "Three";
array[1] = "Four";
this.all[1] = new List(array);
System.out.println(this.all[0].words[0]);
System.out.println(this.all[0].words[1]);
System.out.println(this.all[1].words[0]);
System.out.println(this.all[1].words[1]);
}
public static void main(String[] args) {
Test test = new Test();
}
}
的問題是在控制檯打印出:
Three
Four
Three
Four
我該如何解決這個問題?我需要的實際代碼是以這種方式設置的,所以它不會有太大的改變。
在此先感謝!
謝謝! 但是代碼是如何垃圾?我試圖學習正確的格式和所有。但不知道在哪裏看! – Diesal11
好吧,我會解釋,因爲你問 - 見編輯問題 – Bohemian
對不起,在哪裏?我看不到任何? – Diesal11