Java 从列表填充二维数组
我有一个包含以下数据的文件:Java 从列表填充二维数组,java,arrays,list,Java,Arrays,List,我有一个包含以下数据的文件: 4 5 0 2 0 1 0.6 0 2 0.2 0 3 0.5 1 3 0.8 2 3 0.3 第一行是节点编号,第二行是边编号,第三行包含具有特殊约束的节点 但是,我使用此代码将其从文件中读入两个列表,值和节点,其中值列表包含边(例如:0 1 0.6),而节点列表分别包含3行(4,5,{0,2})的值 我需要构造两个数组,一个等价于邻接矩阵,如下所示 int graph[][] = { {0, 6, 2, 5},
4
5
0 2
0 1 0.6
0 2 0.2
0 3 0.5
1 3 0.8
2 3 0.3
第一行是节点编号,第二行是边编号,第三行包含具有特殊约束的节点
但是,我使用此代码将其从文件中读入两个列表,值和节点,其中值列表包含边(例如:0 1 0.6),而节点列表分别包含3行(4,5,{0,2})的值
我需要构造两个数组,一个等价于邻接矩阵,如下所示
int graph[][] = {
{0, 6, 2, 5},
{6, 0, 0, 8},
{2, 0, 0, 3},
{5, 8, 3, 0}
};
另一个数组是特殊节点id,即
int special [] ={0,2};
我可以从文件中读取值,并将它们按以下形状分为两个列表节点和值:
节点列表包含:4、5、0、2
值列表包含:0,1,0.6,0,2,0.2,0,3,0.5,1,3,0.8,2,3,0.3
我声明了一个名为graph2的二维数组,以保存来自列表的值。但问题是我找不到从值列表填充数据以适应graph2 2d数组的关系。它必须类似于图形数组,但从该文件开始,它的初始化将是动态的
所以,基本上我需要创建两个数组:一个类似于图形数组(2d数组),另一个类似于特殊数组(1d)。根据文件,我必须这样做。提前谢谢
我的代码:
List<Double> values = new ArrayList<>();
List<Integer> nodes = new ArrayList<>();
int c = 0;
File file = new File("text.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
if (c <= 2) {
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().length() > 0) {
nodes.add(Integer.parseInt(str[i]));
}
}
c++;
} else {
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().length() > 0) {
values.add(Double.parseDouble(str[i]));
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
System.out.print(e);
}
}
double graph2 [] [] =new double [nodes.get(0)][nodes.get(0)];
List values=new ArrayList();
列表节点=新的ArrayList();
int c=0;
文件=新文件(“text.txt”);
BufferedReader reader=null;
试一试{
reader=newbufferedreader(newfilereader(file));
字符串文本=空;
而((text=reader.readLine())!=null){
if(c0){
add(Integer.parseInt(str[i]);
}
}
C++;
}否则{
字符串[]str=text.split(“”);
对于(int i=0;i0){
add(Double.parseDouble(str[i]);
}
}
}
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
if(读卡器!=null){
reader.close();
}
}捕获(IOE异常){
系统输出打印(e);
}
}
double graph2[][]=新的双精度[nodes.get(0)][nodes.get(0)];
以下代码将从您的文件创建2D数组(而不是您可以轻松创建的邻接贴图)
节点
将仅包含特殊节点,而图2将包含边描述符
(注:我没有测试它,所以如果出现问题,请告诉我)
List values=new ArrayList();
列表节点=新的ArrayList();
int c=0;
文件=新文件(“text.txt”);
BufferedReader reader=null;
double[]图形2=null;
试一试{
reader=newbufferedreader(newfilereader(file));
字符串文本=空;
而((text=reader.readLine())!=null){
//第一行给出了节点数(您将用于创建int[][]图形=新int[nOfNodes][nOfNodes];)
如果(c==0){
numberOfNodes=Integer.parseInt(text.trim());
}
//第二个给出了边的数量
else如果(c==1){
nOfEdges=Integer.parseInt(text.trim());
图2=新的双[nOfEdges][3];
}
//第三是特殊节点列表
//`nodes`现在只包含您的特殊约束节点
else如果(c==2){
字符串[]str=text.split(“”);
对于(int i=0;i0){
add(Integer.parseInt(str[i]);
}
}
}否则{//那么就有了边描述符
字符串[]str=text.split(“”);
对于(int i=0;i0){
graph2[c-4][i]=Double.parseDouble(str[i]);
}
}
}
C++;
}
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
if(读卡器!=null){
reader.close();
}
}捕获(IOE异常){
系统输出打印(e);
}
}
然后,要创建
图形
,您可以将其初始化为零,然后循环图形2
来填充它。我不明白您想做什么,是否要直接从2d数组中的文件中读取?@JoachimHuet请检查我的编辑。您是否为此问题的下一部分创建了新帐户:@notyou是的,因为该帐户达到了问题限制您的约束节点位于节点。get(2)和graph2缺少第二个参数1。您已经在列表中有了完整的值列表,也许您想在填充gridWell之前初始化graph2,首先要感谢,但是这段代码是错误的,这行graph2[c-3][i]=Double.parseDouble(str[i])中有一个错误;线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:5位于javaapplication6.javaapplication6。main@SUBHIALMOHTASIB因为空白,还有一行,所以必须用<代码> C-4<代码>更改<代码> C-3 <代码>。它应该work@SUBHIALMOHTASIB你可以使用我更新的答案,这次我测试了它,它对我有效。
List<Double> values = new ArrayList<>();
List<Integer> nodes = new ArrayList<>();
int c = 0;
File file = new File("text.txt");
BufferedReader reader = null;
double[][] graph2 = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null) {
// The first line gives the number of nodes (You will use to create the int[][] graph = new int[nOfNodes][nOfNodes];)
if (c == 0) {
numberOfNodes = Integer.parseInt(text.trim());
}
// The second one gives the number of edges
else if (c == 1) {
nOfEdges = Integer.parseInt(text.trim());
graph2 = new double[nOfEdges][3];
}
// And third the list of special nodes
// `nodes` will now contains only your special constrained one
else if (c == 2) {
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().length() > 0) {
nodes.add(Integer.parseInt(str[i]));
}
}
} else { // Then you have your edges descriptors
String[] str = text.split(" ");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().length() > 0) {
graph2[c-4][i] = Double.parseDouble(str[i]);
}
}
}
c++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
System.out.print(e);
}
}