Manage strings in Map

不羁的心 提交于 2020-01-05 12:14:50

问题


I'm already first time using Map on java. I'm creating a project on the console where I create Strings with this structure:

String = word1 word2 word3, code;  //This code is a number, can be the same as the 
                                   // Map's key.

Then, each time I create a String like this, I save it into a Map. After creating and saving into the map one or more Strings, I have to be able to show them in the console, or to delete one of them.

The problem I'm getting is that when adding one String to the map, is overwriting the previous one.

In the main class I work this way:

I add a String to the map:

musicmap.add(title, autor, format);

I list the items:

musicmap.list();

I delete one element from the map:

musicmap.delete(code);

The methods add(), list() and delete() are methods defined in other class this way:

Map<Integer, Music> musicMap= new HashMap<Integer, Music>();  //Music is a class 
                                             // where is defined a constructor with the
                                             // structure of the strings

public void add(String title, String autor, String format){
    int max = 0;
    for (Integer mapCode : musicMap.keySet()){
        if (mapCode > max){
            max = mapCode;
        }
    }
    int newCode = max++;
    Music musicItem = new Music(title, autor, format, newCode);
    musicMap.put(newCode, musicItem);
}

public void list(){
    for (Music item : musicMap.values()){
        System.out.println(item.toString());
    }
}

public void delete(int code){
    musicMap.remove(code);
}

The Music instance just calls to other class where is defined a constructor with the elements of the Music List (these are title, autor, format or type and code):

public Music(String title, String autor, String type, int code){
    this.setTitle(title);
    this.setAutor(autor);
    this.setType(type);
    this.setCode(code);

}


回答1:


Change this line
int newCode = max++;
to
int newCode = max + 1;

Your key value is start with 1.

I hope this will help.

Edit :
In your code newCode = max++ assignment is done before increment the max value, which create problem in getting you max key value by code if (mapCode > max) this condition is not execute because both have same value zero (0) every time.




回答2:


Change int newCode = max++; to int newCode = max + 1;

OR

Change int newCode = max++; to int newCode = ++max;

Currently all your keys are 0. This is a hideous bug beacuse it seems right but the assignment here int newCode = max++; happens before the addition max++.

I think that you don't need a Map because you are simulating the behavior of an ArrayList. Change your implementation to something like this:

private List<Music> list = new ArrayList<Music>();

public void add(String title, String autor, String format){
    Music musicItem = new Music(title, autor, format, newCode);
    list.add(musicItem);
}

you will get your indexes that way. Avoid using the ++ and -- operators in general.



来源:https://stackoverflow.com/questions/20372065/manage-strings-in-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!