gpt4 book ai didi

java - 如何将关系输入二维数组?

转载 作者:行者123 更新时间:2023-12-02 07:44:44 25 4
gpt4 key购买 nike

我的项目数学有问题。

我的项目是编写一个程序来读取一组元素及其关系。您的输入数据将来自文本文件。 (设置关系)。

{1,2,3} {(1,1),(2,2),(3,3),(1,2),(1,3),(2,3)}

我将文本文件读入程序没有问题,但当我想尝试将关系放入二维数组时,我遇到了困难。

例如:{(1,1),(2,2),(3,3),(1,2),(1,3),(2,3)}

二维数组必须是这样的:

col[0][1][2]
[0] 1 1 1
[1] 1 1
[2] 1

我不知道如何将一设置为二维数组,因为文本文件中有各种关系。

这是我的编码。

import javax.swing.*;
import java.util.ArrayList;
import java.io.*;
import java.util.StringTokenizer;
import java.lang.*;
import java.util.*;

public class tests
{
public static int s1[][];
public static int s2[][];
public static int s3[][];
public static int s4[][];
public static int s5[][];
public static int s6[][];
public static int s7[][];
public static int s8[][];
public static int s9[][];
public static int s10[][];

public static void main(String[] args) throws IOException, FileNotFoundException
{
BufferedReader infile = null;

ArrayList arr1 = new ArrayList();
ArrayList arr2 = new ArrayList();
ArrayList arr3 = new ArrayList();
ArrayList arr4 = new ArrayList();
try
{
infile = new BufferedReader (new FileReader ("numbers.txt"));

String indata = null;

while ((indata = infile.readLine())!= null)
{
StringTokenizer st = new StringTokenizer(indata," ");
String set = st.nextToken();
arr1.add(set);
String relation = st.nextToken();
arr2.add(relation);
}

for(int i =0; i < arr2.size(); i++)
{
String r = arr2.get(i).toString();
String result = r.replaceAll("[{}(),; ]", "");
arr3.add(result);
}

for(int i = 0; i < arr3.size(); i++)
{
System.out.println(arr3.get(i).toString());
}

for(int i =0; i < arr1.size(); i++)
{
String s = arr1.get(i).toString();
String result = s.replaceAll("[{}(),; ]", "");
arr4.add(result);
}

int set1 = Integer.parseInt(arr4.get(0).toString());
String ss1 = arr4.get(0).toString();
int a = ss1.length();
s1 = new int[a][a];
int sA[][];
/*for(int row=1;row< a;row++)
{
for(int col=0;col < a;col++)
{
sA = new int[row][col];
int firstNo = Integer.parseInt(arr3.get(row).toString());
int secondNo = Integer.parseInt(arr3.get(col).toString());
sA = new int [firstNo][ secondNo] ;

System.out.print(sA);
}
System.out.println();
}*/
char arrA;
char indOdd=' ',indEven=' ';
char[] cArr = arr3.get(0).toString().toCharArray();
//System.out.println(arr3.get(0).toString().length());
int l = arr3.get(0).toString().length();
int arr10[][] = new int[(l/2)][2];

for(int i=0;i< 2;i++)
{
for(int row = 0; row < (l/2);row++)
{
for(int gh = 0;gh < l;gh++)
{
if(i%2==0)
{
indEven = cArr[gh];
System.out.println(indEven);
arr10[row][i] = indEven;
//System.out.println(arr10[row][i]);
//row++;
}
else
{
indOdd = cArr[gh+1];
System.out.println(indOdd);
arr10[row][i] = indOdd;
//row++;
}
}
}
//arr10 = new int[indOdd][indEven];
//System.out.println(arr10);
}

}
catch (FileNotFoundException fnfe)
{
System.out.println("File not found");
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

infile.close();


}
}

但是如果关系是 {(a,b),(a,c),(b,a),(b,c),( c,c)};{(33,33),(45,45),(67,67),(77,77),(78,78)};

最佳答案

因此,您有两个问题:解析输入和设置数组。

要解析输入,请考虑给定的格式。一个左大括号,一堆有序对,然后是一个右大括号。考虑一下这个伪代码:

Read in a left curly brace
While the next character is not a right curly brace{
Read in a left parenthesis
Get the first number and put it in a variable!
Read in a comma
Get the second number and put it in a variable!
Read in a right parenthesis
Store your relation in the array!
}

现在你的问题只是如何将其放入数组中。您的关系实际上已经被索引到网格中!请注意 0 索引,因此只需从两者中减去 1,并将结果坐标设置为等于 1。

array[first number-1][second number-1]=1;

关于java - 如何将关系输入二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11066749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com