-1
マトリックスの生成中に動的データ構造を使用する必要がありますが、最終的にマトリックスはリストのように表示されますが、これは非常に不快です。 そのため、私は2D配列を追加することにしました。 これを起動すると、ArrayIndexOutOfBoundsException:0エラーが発生します。すべてを試しましたが、それでも動作しません。どのようにそれを修正することができます任意のアイデア?あなたの配列の長さが0で、あなたには、ArrayIndexOutOfBoundsExceptionを誘発する理由どのようにArrayIndexOutOfBoundsException:0を修正できますか?
List<Integer> J = null;
static int ord;
static int ord1;
static int notZero;
static int val=0;
public static int[][] array;
public Matrix(){
int numberCol;
A=new LinkedList<ElementStructure>();
x=new LinkedList<ElementStructure>();
b=new LinkedList<ElementStructure>();
J = new LinkedList<Integer>();
array = new int[ord][ord];
Random rand= new Random();
System.out.print("Enter matrix order ");
Scanner scn = new Scanner(System.in);
ord = scn.nextInt();
ord1=ord/2;
for (int i=0; i<ord;i++){
for(int j=0; j<ord; j++){
if(j==ord-i-1){
J.add(j);
int q = rand.nextInt(201)-100;
A.add(new ElementStructure(i,j,q));
array[i][j] = q;
val++;
break;
} else if(ord<20){notZero=ord/2;}
else { notZero=rand.nextInt(ord);}
for(int k=0; k<notZero; k++){
numberCol=rand.nextInt(ord);
if(J.contains(numberCol)){continue;}
J.add(numberCol);
int q = rand.nextInt(201)-100;
A.add(new ElementStructure(i,numberCol, q));
array[i][numberCol] = q;
val++;
}
break;
}
}
}
public static void main(String[] args) throws IOException {
Matrix Matrix = new Matrix();
for (int c=0; c<Matrix.val; c++){
System.out.println(Matrix.A.get(c).toString());
}
for (int i = 0;i < ord;i++){
for (int j = 0;j < ord;j++){
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
配列の長さはゼロです。配列を最初に初期化した後は、サイズを変更することはできません。あなたは '[0] [0]'で初期化します –
ありがとう、コードは今動作しています –