本文介绍了添加和在HashMap中删除的ArrayList集合中的元素作为一种价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个看起来像这样的文件:

So I have a file that looks like this:

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

在这里我想的第一列是一个HashMap的键(E1或E2,或E3),该值被称为等级一个ArrayList,在我想要的第二列有它的值( INT),ArrayList的第n个指标内。

where I want the first column to be the key of a hashmap (e1, or e2, or e3), and the value to be an ArrayList called "Ratings" that in I want the second column to have it's value (an int), inside the nth index of the arraylist.

下面是我的code,在它的全部迄今:

Here is my code in it's entirety so far:

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的内容存在问题。

I am having a problem with modifying the contents of the arraylist.

要总结一下:
在文件的第1列每串会对它在HashMap中自己的密钥(用户列表)
该程序将检查它是否具有关键,如果没有密钥存在,它会创建一个新的ArrayList作为键的值。
该ArrayList将与50的索引,其中,它们将包含0来填充。
在这之后,数组列表将具有在其中在第二列中的整数将在第n列的相应值被添加的文件添加新的值。

To sum up:Every string in the 1st column of the file would have it's own key in the hashmap (userList)The program will check if it has the key, if no key exists, it will create a new arraylist as the value for the key.The arrayList will be populated with 50 indexes, of which they will contain "0".After that, the arraylist will have new values added from the file in which the integer in the second column will be added at the corresponding value of the nth column.

我将如何填充ArrayList和我将如何修改它,这样如果我想用户E6的第n个指标,我可以?

How would I populate the arraylist, and how would I edit it so that if I want to add a new integer at the nth index of user e6, I could?

推荐答案

在给定的密钥是不是在你的地图present必须添加一个新的键值对

When given key is not present in your Map you have to add a new Key value pair

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]); 这是不行的给你的文件的内容会抛出 NumberFormatException的为V1不能被解析为int。

iData[i] = Integer.parseInt(sData[i]); This will not work given your file content it will throw NumberFormatException as v1 cannot be parsed to int.

相反,你可以做这样的事情:

Instead you can do something like this:

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());

这篇关于添加和在HashMap中删除的ArrayList集合中的元素作为一种价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:40