怎么遍历HashMap中的ArrayList,运行结果乱码了,程序在下面。求大神支援
importjava.util.*;importjava.util.Map.Entry;publicclassMovie3{intmovieId;StringmovieN...
import java.util.*;
import java.util.Map.Entry;
public class Movie3 {
int movieId;
String movieName;
double price;
public Movie3(int movieId, String movieName,double price) {
this.movieId = movieId;
this.movieName = movieName;
this.price = price;
}
public int getID(){
return movieId;
}
public String getMovieName(){
return movieName;
}
public double getPrice(){
return price;
}
public static void main(String[] args){
Movie3 a=new Movie3(1,"冰雪奇缘",40);
Movie3 b=new Movie3(2,"美国队长2",40);
Movie3 c=new Movie3(3,"大闹天宫",50);
Movie3 d=new Movie3(4,"卑鄙的我2",50);
List sciencemovieDVDList=new ArrayList();
List AnimationmovieDVDList=new ArrayList();
sciencemovieDVDList.add(b);
sciencemovieDVDList.add(c);
AnimationmovieDVDList.add(a);
AnimationmovieDVDList.add(d);
Map movies=new HashMap();
movies.put("科幻电影",sciencemovieDVDList);
movies.put("动漫电影",AnimationmovieDVDList);
System.out.println("键的集合" + movies.keySet());
System.out.println("键-值对集合:" + movies);
String key = "科幻电影";
if (movies.containsKey(key)) {
System.out.println("键“"+key+"”对应的值:"+movies.get(key));
}
movies.remove(key);
System.out.println("键-值”对集合:"+ movies);
}
} 展开
import java.util.Map.Entry;
public class Movie3 {
int movieId;
String movieName;
double price;
public Movie3(int movieId, String movieName,double price) {
this.movieId = movieId;
this.movieName = movieName;
this.price = price;
}
public int getID(){
return movieId;
}
public String getMovieName(){
return movieName;
}
public double getPrice(){
return price;
}
public static void main(String[] args){
Movie3 a=new Movie3(1,"冰雪奇缘",40);
Movie3 b=new Movie3(2,"美国队长2",40);
Movie3 c=new Movie3(3,"大闹天宫",50);
Movie3 d=new Movie3(4,"卑鄙的我2",50);
List sciencemovieDVDList=new ArrayList();
List AnimationmovieDVDList=new ArrayList();
sciencemovieDVDList.add(b);
sciencemovieDVDList.add(c);
AnimationmovieDVDList.add(a);
AnimationmovieDVDList.add(d);
Map movies=new HashMap();
movies.put("科幻电影",sciencemovieDVDList);
movies.put("动漫电影",AnimationmovieDVDList);
System.out.println("键的集合" + movies.keySet());
System.out.println("键-值对集合:" + movies);
String key = "科幻电影";
if (movies.containsKey(key)) {
System.out.println("键“"+key+"”对应的值:"+movies.get(key));
}
movies.remove(key);
System.out.println("键-值”对集合:"+ movies);
}
} 展开
1个回答
展开全部
乱码?????你加个tostring方法,不是乱不乱码,一看就是菜鸟
package image;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Movie3
{
int movieId;
String movieName;
double price;
public Movie3 ( int movieId, String movieName, double price )
{
this.movieId = movieId;
this.movieName = movieName;
this.price = price;
}
public int getID ()
{
return movieId;
}
public String getMovieName ()
{
return movieName;
}
public double getPrice ()
{
return price;
}
@Override
public String toString ()
{
StringBuilder builder = new StringBuilder ();
builder.append ("Movie3 [movieId=");
builder.append (movieId);
builder.append (", movieName=");
builder.append (movieName);
builder.append (", price=");
builder.append (price);
builder.append ("]");
return builder.toString ();
}
public static void main ( String[] args )
{
Movie3 a = new Movie3 (1, "冰雪奇缘", 40);
Movie3 b = new Movie3 (2, "美国队长2", 40);
Movie3 c = new Movie3 (3, "大闹天宫", 50);
Movie3 d = new Movie3 (4, "卑鄙的我2", 50);
List sciencemovieDVDList = new ArrayList ();
List AnimationmovieDVDList = new ArrayList ();
sciencemovieDVDList.add (b);
sciencemovieDVDList.add (c);
AnimationmovieDVDList.add (a);
AnimationmovieDVDList.add (d);
Map movies = new HashMap ();
movies.put ("科幻电影", sciencemovieDVDList);
movies.put ("动漫电影", AnimationmovieDVDList);
System.out.println ("键的集合" + movies.keySet ());
System.out.println ("键-值对集合:" + movies);
String key = "科幻电影";
if (movies.containsKey (key))
{
System.out.println ("键“" + key + "”对应的值:" + movies.get (key));
}
movies.remove (key);
System.out.println ("键-值”对集合:" + movies);
}
}
追问
能解释下那个to string方法吗?大神,我刚学JAVA
追答
tostring方法就是指将一些模型信息打印成有条理的内容,不是原始的Object的tostring方法,原始的没有意义,所以就是乱码,然后一般情况下,只要有setter和getter的model数据类对象时,都要有tostring方法的覆盖,返回该类有意义的内容,详见:java十大低级错误
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询