簡體   English   中英

在ArrayList中添加和刪除元素設置為哈希圖中的值

[英]Adding and deleting elements in ArrayList set as a value in a hashmap

所以我有一個看起來像這樣的文件:

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

在這里我希望第一列是哈希圖的鍵(e1,e2或e3),值是一個名為“ Ratings”的ArrayList,在其中我希望第二列具有它的值(一個int),在arraylist的第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)
        {
        }
    }
}

我在修改arraylist的內容時遇到問題。

總結一下:文件的第一列中的每個字符串在hashmap(userList)中都有其自己的鍵。程序將檢查它是否具有鍵,如果不存在鍵,它將創建一個新的arraylist作為該值。鍵。 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); 

同樣為了引用使用,接口代替實現類,如下所示:

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

如果您的地圖中沒有指定的鍵,則必須添加一個新的鍵值對

if(!userRatings.containsKey(user)) //The user does not have ratings.
{
   ArrayList<Integer> ratings = new ArrayList<Integer>();                   
   for (int j = 0; j < 50; j++) {
       ratings.add(j);
  }
  userRatings.put(user, ratings); // place new mapping 
  System.out.println(user + " " + userRatings.get(user));
} else //The user has ratings
{
    ArrayList<Integer> ratings  = userRatings.get(user);
    ratings.add(location,value); // Update your list with location and value
    System.out.println(user + " " + userRatings.get(user)); 
}

iData[i] = Integer.parseInt(sData[i]); 給定您的文件內容,此操作將不起作用,因為v1無法解析為int,它將引發NumberFormatException

相反,您可以執行以下操作:

value = Integer.parseInt(sData[1].trim().substring(1));
location = Integer.parseInt(sData[2].trim());

比較兩個鍵的值:

方法1

ArrayList<Integer> first = userRatings.get(e1);
ArrayList<Integer> second = userRatings.get(e2);

//Taking the smallest size will ensure that we don't get IndexOutOfBoundsException.

int length = first.size() < second.size() ? first.size() : second.size();

for(int iDx = 0; iDx < legth; iDx++){
   //compare content
}

方法2

ArrayList<Integer> first = userRatings.get(e1);
ArrayList<Integer> second = userRatings.get(e2);

Arrays.equals(first.toArray(), second.toArray());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM