我需要一些帮助来理解代码。我正在努力解决这个问题。 我很好奇为什么在输出中有逐个值" D15,D10,D5和#34;。我知道我们持有价值观' x'在列表上,但为什么有字母" D",这是在" toString"方法。我看不到任何地方,它被带到哪里。这是"这是"在B.make()中添加(这个)?如果有人能帮助我,我真的很感激。
输出:H1 G0 C B A F15 G0 C A F10 G0 C A F5 C D15 D10 D5(新行)
public class Main {
public static void main(String[] args) {
int mat[][] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
try {
fun(mat, 0, 0);
} catch (Exception ex) {
System.out.println(ex);
}
A a = new A(15);
new A(10);
new A(5);
B.make().show();
}
public static void fun(int[] tab, int i, int j){
for(int k = 0; k<i;k++){
System.out.print("I"+tab[k]+" , ");
}
System.out.println();
}
public static int fun(int[][] tab, int i, int j) {
if (i == 0 && j == 0) {
System.out.println("H" + tab[i][j]);
return tab[i][j] + fun(tab, i - 1, j + 1);
} else
return 0;
}
}
class A {
int x;
A next;
private A() {
System.out.println("G" + x);
B.make().add(this);
}
public A(int x) {
this();
this.x = x;
System.out.println("F" + x);
}
public A(A objA) {
this(objA.x - 1);
System.out.println("E" + x);
}
public String toString() {
return "D" + x;
}
}
class B {
private static B struct;
public static B make() {
System.out.println("C");
if (struct == null)
struct = new B();
return struct;
}
private A head;
private B() {
head = null;
System.out.println("B");
}
public void add(A obj) {
if (head == null) {
head = obj;
} else {
A tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
tmp.next = obj;
}
System.out.println("A");
}
public void show() {
A tmp = head;
while (tmp != null) {
System.out.println(tmp);
tmp = tmp.next;
}
}
}