gpt4 book ai didi

java - 添加和删​​除 ArrayList 中设置为 hashmap 中的值的元素

转载 作者:行者123 更新时间:2023-12-01 15:21:19 25 4
gpt4 key购买 nike

所以我有一个如下所示的文件:

    1st 2nd­ nth
e1­, ­­v1, 1
e1, v3, 2
e1, v4, 4
e1, v5, 7
e2, v1, 1
., ­., .
., ­., .
., ­., .

我希望第一列是 HashMap 的键(e1、e2或e3),值是一个名为“Ratings”的ArrayList,我希望第二列具有它的值(一个int),位于数组列表的第 n 个索引内。

这是迄今为止我的完整代码:

import java.util.*;
import java.io.*;

public class Setup
{
public static void Setup(String[] args)
{
String user;
int value, location;
//Create a Hashmap that holds a string key and an ArrayList value
HashMap<String, ArrayList<Integer>> userRatings = new HashMap<String, ArrayList<Integer>>();
try
{
BufferedReader bufferReader = new BufferedReader(new FileReader("Student list.txt")); //read from file
String line, sentence; //declare two string variables
String[] sData; //declare a string array (store the contents here)
line = bufferReader.readLine(); //Read the line
while (line != null) //While there is a line, do this:
{
line = bufferReader.readLine();
sData = line.split(", "); //Into the string array, enter individual values in the line split by the ", " characters
int iData[] = new int[sData.length]; //Create an int array the size of the string array
user = sData[0];
for (int i = 0; i <sData.length; i++) //fill the int array with the int-version of the string array
{
iData[i] = Integer.parseInt(sData[i]); //pass the strings as integers into the integer array
}
value = iData[1];
location = iData[2];
if(!userRatings.containsKey(user)) //The user does not have ratings.
{
ArrayList<Integer> ratings = new ArrayList<Integer>();
// ratings = userRatings.get(user);
for (int j = 0; j < 50; j++)
{
ratings.add(j);
}
System.out.println(user + " " + userRatings.get(user));

}
else //The user has ratings
{
userRatings.get(user).add(location,value);
System.out.println(user + " " + userRatings.get(user));
}
}
bufferReader.close();
} catch (FileNotFoundException e)
{
System.out.println("File does not exist or could not be found.");
}
catch (IOException e)
{
System.out.println("Can't read from file");
}
catch (NullPointerException e)
{
}
}
}

我在修改数组列表的内容时遇到问题。

总结一下:文件第一列中的每个字符串在 HashMap (userList) 中都有自己的键程序将检查它是否有该键,如果不存在,它将创建一个新的数组列表作为该键的值。arrayList 将填充 50 个索引,其中包含“0”。之后,arraylist 将从文件中添加新值,其中第二列中的整数将添加到第 n 列的相应值中。

我将如何填充数组列表,以及如何编辑它,以便如果我想在用户 e6 的第 n 个索引处添加一个新整数,我可以?

最佳答案

我认为您可以只读取第 n 个值并使用它将该值插入列表中。

例如

List<Integer> list = new ArrayList<Integer>();
//values values (0) will be inserted into the list
//after that for each value in the nth you do something as follows
//i is the value in the nth column, if the least value for i is 1 otherwise just use i
list.add(i-1, value);

同样为了引用使用,接口(interface)而不是实现类如下 -

Map<String, List<Integer>> userRatings = new HashMap<String, List<Integer>>();
List<Integer> ratings = new ArrayList<Integer>();

将值插入到用户 e6 的第 n 个索引中:

List<Integer> ratings = userRatings.get(user); 
if(ratings == null) {
ratings = new ArrayList<Integer>(); //it's not in the map yet
//insert 50 0s here
userRatings.put(user, ratings);
}
ratings.add(i, value); //i is the nth index and value is the rating

关于java - 添加和删​​除 ArrayList 中设置为 hashmap 中的值的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875746/

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