나는 문제가있다. 그리고 나는 이것에 관해 전혀 모른다. 그래서 나는 유사한 코딩을하고, 나의 질문을 게시한다, 사람이 나를 도울 수 있기를 바란다.데이터를 다른 객체에 저장하지만 덮어 쓴다
package test;
import java.util.ArrayList;
import java.util.List;
public class newclass {
private Object[][] array;
private int x, y;
List<Integer> slot = new ArrayList<>();
public newclass(Object[][]array, int x, int y){
this.array = array;
this.x = x;
this.y = y;
}
public void storedata(){
int i=0;
while(i < 2){
int a,b;
a=(int) Math.floor(Math.random()*(x));
b=(int) Math.floor(Math.random()*(y));
if(NC[a][b]==null){
slot.add(0);
NC[a][b] = slot;
slot = new ArrayList<>();
}
else{
List templist = (List)NC[a][y];
templist.add(0);
}
i++;
}
}
public void display(){
for(int i=0; i<x ;i++){
for(int j=0; j<y ;j++){
List templist = (List)array[i][j];
if(templist==null){
System.out.print("");
}
else{
for (int k = 0; k < templist.size(); k++) {
System.out.print(templist.get(k));
}
}
System.out.print(",");
}
System.out.print("\n");
}
}
}
내 문제는 덮어 메인 클래스에서 두 번째 루프에있는 "0 |":이 내 새로운 클래스가
package test;
public class Test {
public static void main(String[] args) {
newclass [] NC;
NC = new newclass[2];
Object[][] array = new Object[5][5];
int x=5, y=5;
for (int i=0 ; i<2 ; i++){
NC[i]= new newclass(array, x, y);
}
//solve roomHC
for (int i=0 ; i<2 ; i++){
NC[i].storedata();
}
//display solution
for (int i=0 ; i<2 ; i++){
System.out.print("\n");
NC[i].display();
System.out.print("========================");
}
}
}
입니다
이 내 메인 클래스입니다 배열에 다시 입력하십시오.
어떻게 당신이 이제까지 일관된 결과를 기대할 수'인 Math.random()는'a와 b를 설정하는 방법? 또한 'slot = new ArrayList <>();'는 루프의 각 반복문을 다시 초기화하여 어떤 값도 보유하지 않도록한다는 것을 의미합니다. – hooda
내 질문을 편집 했으므로 다시 점검 할 수 있습니다 .. – user3832964
이제 Math.random()을 제거 했으므로 두 객체 모두에서 동일한 결과가 나타납니다. 두 개체의 두 번째 열에 0을 넣을뿐입니다. 당신이 여기서 무엇을하려하는지 명확하게 알지 못합니다. – RealSkeptic