从java数组解释数据
我有一个任务,要求我获取一个大型数据集,将其存储在一个数组中,然后创建以各种方式解释数据的方法。我收到的文件数据格式如下: 0138从java数组解释数据,java,arraylist,java.util.scanner,Java,Arraylist,Java.util.scanner,我有一个任务,要求我获取一个大型数据集,将其存储在一个数组中,然后创建以各种方式解释数据的方法。我收到的文件数据格式如下: 0138 0139 0 140 0 141 0 142 0799 4.1 4.10 4.12 4 18 等(非常大) 这些数据应该代表人的社会网络,数字代表个人。每一行都包含一个左边的人,他“信任”右边的人。我应该解释这些数据,这样我就可以找到一个特定的人信任的所有人,有多少人信任一个特定的人,以及如何找到最信任的人。然而,我完全不知道如何编写这些方法,所以我想知道你们是否
0139
0 140
0 141
0 142
0799
4.1
4.10
4.12
4 18
等(非常大) 这些数据应该代表人的社会网络,数字代表个人。每一行都包含一个左边的人,他“信任”右边的人。我应该解释这些数据,这样我就可以找到一个特定的人信任的所有人,有多少人信任一个特定的人,以及如何找到最信任的人。然而,我完全不知道如何编写这些方法,所以我想知道你们是否可以帮助我。以下是我目前掌握的代码:
public class SocialNetwork {
static Scanner scanner = new Scanner(System.in);
static void findTrusted()
{
System.out.println("Please input person number you would like to find Trustees for");
trustee = (scanner.next());
}
public static void main(String[] args){
File inData = new File("dataset.txt");
ArrayList<Integer> links = new ArrayList<Integer>();
try
{
Scanner in = new Scanner(inData);
in.nextLine();
in.nextLine();
in.nextLine();
in.nextLine();
while (in.hasNext())
{
int trustee = in.nextInt();
int trusted = in.nextInt();
links.add(trustee);
links.add(trusted);
}
in.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
公共类社交网络{
静态扫描仪=新扫描仪(System.in);
静态无效findTrusted()
{
System.out.println(“请输入您希望找到受托人的人员编号”);
受信者=(scanner.next());
}
公共静态void main(字符串[]args){
File inData=新文件(“dataset.txt”);
ArrayList links=新的ArrayList();
尝试
{
扫描仪输入=新扫描仪(inData);
in.nextLine();
in.nextLine();
in.nextLine();
in.nextLine();
while(在.hasNext()中)
{
int truster=in.nextInt();
int trusted=in.nextInt();
新增(受托人);
添加链接(受信任);
}
in.close();
}
catch(filenotfounde异常){
e、 printStackTrace();
}
}
}
正如您所看到的,我的findTrustee方法几乎没有什么内容。我只是不知道从哪里开始。我提出了一个小伪代码,试图剖析需要做什么:
然而,我只是不太知道如何做到这一点 结构
链接
并不能真正帮助您。它不知道“从”和“到”。您将人员存储为数字,但不存储两个人之间的任何关系。您实际上是在从事图论工作,如果可以的话,您应该看看图论的参考著作和Java库
那么,什么是信任链接?它是一个有两个人的对象,受托人和受信任的人。为此创建一个类:
public class Trust {
private final int trustee;
private final int trusted;
public Trust(final int trustee, final int trusted) {
this.trustee = trustee;
this.trusted = trusted;
}
// Getters, equals, hashCode, toString, formatted output for humans.
}
让你的班级SocialNetwork
能够创建这些。顺便说一句,在main方法中创建一个SocialNetwork实例,并停止对其他所有内容使用static
public Trust createTrust(Scanner scanner) {
int trustee = scanner.nextInt();
int trusted = scanner.nextInt();
return new Trust(trustee, trusted);
}
您可能需要添加异常处理和文件结束处理
制作链接
一个信任
对象的列表,然后根据需要编写扫描该列表的方法
/**
Return a list of all the people who trustee trusts.
@param trustee A person in the system.
@return a list of the people trustee trusts.
*/
public List<Integer> trusting(int trustee) {
final List<Integer> trusted = new ArrayList<>();
for (Trust link: links) {
// Add something from link to trusted if it should.
// This looks like homework; I'm not doing everything for you.
}
return trusted;
}
/**
返回受托人信任的所有人的列表。
@param受托人系统中的一个人。
@返回受托人信托的人员列表。
*/
公共列表信任(int受托人){
最终信任列表=新的ArrayList();
用于(信任链接:链接){
//如果应该,请将链接中的内容添加到受信任的内容。
//这看起来像是家庭作业,我不是为你做一切。
}
返回信任;
}
根据需要编写其他方法。然后,考虑这些数据结构对于这个问题是否有效。
Map
s会更好吗<代码>多重映射s是否来自其他库?某种开源图论库?也许你应该改用数据库。也许你应该有一个Person
类,而不是只使用整数;这样你就可以给人们贴上他们的名字。链接的结构对你没有帮助。它不知道“从”和“到”。您将人员存储为数字,但不存储两个人之间的任何关系。您实际上是在从事图论工作,如果可以的话,您应该看看图论的参考著作和Java库
那么,什么是信任链接?它是一个有两个人的对象,受托人和受信任的人。为此创建一个类:
public class Trust {
private final int trustee;
private final int trusted;
public Trust(final int trustee, final int trusted) {
this.trustee = trustee;
this.trusted = trusted;
}
// Getters, equals, hashCode, toString, formatted output for humans.
}
让你的班级SocialNetwork
能够创建这些。顺便说一句,在main方法中创建一个SocialNetwork实例,并停止对其他所有内容使用static
public Trust createTrust(Scanner scanner) {
int trustee = scanner.nextInt();
int trusted = scanner.nextInt();
return new Trust(trustee, trusted);
}
您可能需要添加异常处理和文件结束处理
制作链接
一个信任
对象的列表,然后根据需要编写扫描该列表的方法
/**
Return a list of all the people who trustee trusts.
@param trustee A person in the system.
@return a list of the people trustee trusts.
*/
public List<Integer> trusting(int trustee) {
final List<Integer> trusted = new ArrayList<>();
for (Trust link: links) {
// Add something from link to trusted if it should.
// This looks like homework; I'm not doing everything for you.
}
return trusted;
}
/**
返回受托人信任的所有人的列表。
@param受托人系统中的一个人。
@返回受托人信托的人员列表。
*/
公共列表信任(int受托人){
最终信任列表=新的ArrayList();
用于(信任链接:链接){
//如果应该,请将链接中的内容添加到受信任的内容。
//这看起来像是家庭作业,我不是为你做一切。
}
返回信任;
}
根据需要编写其他方法。然后,考虑这些数据结构对于这个问题是否有效。
Map
s会更好吗<代码>多重映射s是否来自其他库?某种开源图论库?也许你应该改用数据库。也许你应该有一个Person
类,而不是只使用整数;这样你就可以给人们贴上他们的名字。我认为有很多方法可以实现这一点(不管性能如何)。例如,您可以使用HashMap、array of array(或者list of list,如果您真的喜欢list…)
我将举一个使用列表的例子,也许,因为你似乎在使用它。。。(虽然我觉得这有点奇怪)
比如说,你有一张名单