我上了这堂课:
import java.util.*;
public class MatrixArrayList extends AbstractMatrix {
private ArrayList<ArrayList<Integer>> values;
public MatrixArrayList(int nbl, int nbc) {
super(nbl, nbc);
values=new ArrayList<ArrayList<Integer>>();
}
@Override
public int getValue(int x, int y) {
return values.get(x).get(y) ;
}
@Override
public void setValue(int x, int y, int value) {
values.get(x).set(y, value);
}
}
我得到了
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
我想制作一个ArrayList
的矩阵
我有这个问题:
valeurs.get(x).set(y,valeur);
答案 0 :(得分:0)
从您的代码中不清楚哪个调用会导致异常,但很明显您没有初始化内部数组列表,也没有向其添加任何对象。
在构造函数中初始化它时:
values=new ArrayList<ArrayList<Integer>>();
您基本上做的是将values
对象实例化为大小为0的ArrayList(尚未添加任何项目)。
然后,如果您在索引setValue
处进行0
,则会得到例外情况,因为当您执行get(x)
时,您基本上正在执行get(0)
1}}在0的集合上 - 没有什么可以得到的,你超出了数组的范围。
您可能想要做的是初始化构造函数中的所有数组:
public MatrixArrayList(int nbl, int nbc) {
super(nbl, nbc);
values=new ArrayList<ArrayList<Integer>>(nbl);
for (int i = 0; i < nbl; i++) {
values.add(new ArrayList<Integer>(nbc));
}
}
然后您可以在getValue
或setValue
中毫无问题地访问它们(如果您确实超出了界限,或者您还没有设置任何问题,那么您将获得此异常在特定指数的价值。见下面的评论:)。
请注意,由于您使用的是ArrayList
对象,而不是原始的int[]
数组,因此您的数组中仍然没有值。简单地执行new ArrayList<Integer>()
甚至new ArrayList<Integer>(num)
仍会留下大小为0的列表。如果要覆盖所有基础,可能需要初始化ArrayList,或者在每个{{{{{{ 1}}您可以使用get
或getValue
。
答案 1 :(得分:0)
new ArrayList<ArrayList<Integer>>()
创建一个空列表。要使用值填充该列表,您需要调用add()
。
假设您要使用nbl
空值nbc
列表填充矩阵:
this.values = new ArrayList<>(nbl);
for (int i = 0; i < nbl; i++) {
ArrayList<Integer> row = new ArrayList<>(nbc);
for (int j = 0; j < nbc; j++)
row.add(null);
this.values.add(row);
}
如果需要,您还可以使用0
值填充它。
当然,如果矩阵无法改变大小,那么创建一个简单的数组版本可能要好得多,而不是你试图创建的ArrayList
版本。
答案 2 :(得分:0)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Shell {
static List<ArrayList<ArrayList<Double>>> read(String filename) {
ArrayList<ArrayList<Double>> A = new ArrayList<ArrayList<Double>>();
ArrayList<ArrayList<Double>> B = new ArrayList<ArrayList<Double>>();
String thisLine;
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
// Begin reading A
while ((thisLine = br.readLine()) != null) {
if (thisLine.trim().equals("")) {
break;
} else {
ArrayList<Double> line = new ArrayList<Double>();
String[] lineArray = thisLine.split("\t");
for (String number : lineArray) {
line.add((double) Integer.parseInt(number));
}
A.add(line);
}
}
// Begin reading B
while ((thisLine = br.readLine()) != null) {
ArrayList<Double> line = new ArrayList<Double>();
String[] lineArray = thisLine.split("\t");
for (String number : lineArray) {
line.add((double) Integer.parseInt(number));
}
B.add(line);
}
} catch (IOException e) {
System.err.println("Error: " + e);
}
List<ArrayList<ArrayList<Double>>> res = new LinkedList<ArrayList<ArrayList<Double>>>();
res.add(A);
res.add(B);
return res;
}
static int[][] ijkAlgorithm(ArrayList<ArrayList<Integer>> A,
ArrayList<ArrayList<Integer>> B) {
int n = A.size();
// initialise C
int[][] C = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
C[i][j] += A.get(i).get(k) * B.get(k).get(j);
}
}
}
return C;
}
static void printMatrix(Matrix matrix, int n) {
for (int i = 0; i < n; i++) {
StringBuilder sb = new StringBuilder(matrix.length);
for (int j = 0; j < n; j++) {
if (j != 0) {
sb.append("\t");
}
String formattedString = String.format("%.0f", matrix.get(i, j))
sb.append(formattedString);
}
System.out.println(sb.toString());
}
}
public static void main(String[] args) {
String filename;
if (args.length < 2) {
filename = "2000.in";
} else {
filename = args[1];
}
List<ArrayList<ArrayList<Double>>> matrices = read(filename);
ArrayList<ArrayList<Double>> A = matrices.get(0);
ArrayList<ArrayList<Double>> B = matrices.get(1);
int n = A.size();
double[][] Aarray = new double[n][n];
double[][] Barray = new double[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Aarray[i][j] = A.get(i).get(j);
Barray[i][j] = B.get(i).get(j);
}
}
Matrix AM = new Matrix(Aarray);
Matrix BM = new Matrix(Aarray);
Matrix CM = AM.times(BM);
printMatrix(CM, n);
}
}
包含您要执行此操作的矩阵的输入文件
希望这段代码有助于编码。