java哈希表学生成绩_JAVA中学生的哈希表

我正在尝试制作学生的哈希表。文本文件中的数据按行排列:

第1行(及其后的奇数行):学生的LDAP标识符,

名和姓(用空格分隔)。

第2行(及随后的双行):主题代码的顺序未排序。这些代码是普通数字,并用空格分隔。

因此,我尝试使用HashMap类,设计一个哈希表来存储所有学生信息。密钥将是学生的LDAP字符串,主题代码将存储在有序列表中。

首先,我完成了main.java:

package hashMap;

import java.io.IOException;

public class Main {

public static void main (String[] args) throws IOException {

HashTaula table = new HashTaula("file/students.txt");

table.print();

}

}

InfoItem.java:

package hashMap;

import java.util.ArrayList;

public class InfoItem {

String name;

String surname;

ArrayList subjectscode;

public InfoItem() {

}

public InfoItem(String name, String surname) {

this.name = name;

this.surname = surnmae;

subjectscode = new ArrayList();

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getSurname() {

return surname;

}

public void setSurname(String surname) {

this.surname = surname;

}

public ArrayList getSubjectscode() {

return subjectscode;

}

public void setSubjectscode(ArrayList subjectscode) {

this.subjectscode = subjectscode;

}

}

And hashTable.java, here is the problem. It gives me NullPointer in table.put (ldap, item);.

package hashMap;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.HashMap;

public class HashTable {

HashMap table;

public HashTable() {

table = new HashMap();

}

public HashTable (String fileName) throws java.io.IOException {

File file = new File(fileName);

FileReader input = new FileReader(file);

BufferedReader reader = new BufferedReader(input);

String line = reader.readLine();

int kont = 0;

while (line != null) {

String[] info = line.trim().split("[\\s,;:.\"]+");

InfoItem item = new InfoItem();

String ldap = null;

if (kont%2 == 0) {

ArrayList subjectscode = new ArrayList();

for (int i=0; i

//INSERT IN ORDER

kodeak.add(i);

item.setSubjectscode(subjectscode);

}

else {

ldap = info[1];

item = new InfoItem(info[2], info[3]);

table.put(ldap, item);

}

kont++;

line = reader.readLine();

}

reader.close();

}

public void print() {

for (String i : table.keySet()) {

System.out.println("key: " + i + " value: " + table.get(i));

}

}

}

有人可以帮助我正确实现哈希表吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值