map存的是类对象怎么取对象的值看代码

map存的是类对象怎么取对象的值看代码,第1张

1通过60,80,90取值:类似Student stu60=mapget(60);

2通过迭代器,取所有的数据

Set<Integer> s = mapkeySet();

Iterator<Integer> it = siterator();

while(ithasNext()){

Integer itg = itnext();

Student st = mapget(itg);//st是每一个map中的Student对象(stu1,stu2,stu3)

}

el表达式实际上是java代码, 即jsp里的java代码, 是servlet执行的

也就是说, 后台看到el表达式的时候就执行了, 然后用执行的结果代替这个el表达式, 然后再返回给前台

你写alert("${endtimeMap['8']}");

后台看到的是就是${endtimeMap['8']}这个东西, 然后计算从map里得到对应的值, 代替则个东西, 所以你到前台看到的实际上是map里的值了

如果你写alert("${endtimeMap['"+ff+"']}");, 后台看到的是${endtimeMap['"+ff+"']}, 你觉得后台能把这个解析成什么, 后台会认为你是取map里key值为"\"+ff+\""的value(你要是不信, 你给你的map里添一组这个key-value)

归根结底还是执行顺序的问题, el表达式是servlet是后台执行的, js是得到response后在前台执行的, el优先于js, 你的想法是js先把值穿进去, 然后el表达式再得值, 所以必须不正确了

import javaxswing;

import javaawt;

import javaawtevent;

import javautil;

import combruceeckelswing;

public class TrackEvent extends JApplet {

private HashMap h = new HashMap();

private String[] event = {

"focusGained", "focusLost", "keyPressed",

"keyReleased", "keyTyped", "mouseClicked",

"mouseEntered", "mouseExited", "mousePressed",

"mouseReleased", "mouseDragged", "mouseMoved"

};

private MyButton

b1 = new MyButton(ColorBLUE, "test1"),

b2 = new MyButton(ColorRED, "test2");

class MyButton extends JButton {

void report(String field, String msg) {

((JTextField)hget(field))setText(msg);

}

FocusListener fl = new FocusListener() {

public void focusGained(FocusEvent e) {

report("focusGained", eparamString());

}

public void focusLost(FocusEvent e) {

report("focusLost", eparamString());

}

};

KeyListener kl = new KeyListener() {

public void keyPressed(KeyEvent e) {

report("keyPressed", eparamString());

}

public void keyReleased(KeyEvent e) {

report("keyReleased", eparamString());

}

public void keyTyped(KeyEvent e) {

report("keyTyped", eparamString());

}

};

MouseListener ml = new MouseListener() {

public void mouseClicked(MouseEvent e) {

report("mouseClicked", eparamString());

}

public void mouseEntered(MouseEvent e) {

report("mouseEntered", eparamString());

}

public void mouseExited(MouseEvent e) {

report("mouseExited", eparamString());

}

public void mousePressed(MouseEvent e) {

report("mousePressed", eparamString());

}

public void mouseReleased(MouseEvent e) {

report("mouseReleased", eparamString());

}

};

MouseMotionListener mml = new MouseMotionListener() {

public void mouseDragged(MouseEvent e) {

report("mouseDragged", eparamString());

}

public void mouseMoved(MouseEvent e) {

report("mouseMoved", eparamString());

}

};

public MyButton(Color color, String label) {

super(label);

setBackground(color);

addFocusListener(fl);

addKeyListener(kl);

addMouseListener(ml);

addMouseMotionListener(mml);

}

}

public void init() {

Container c = getContentPane();

csetLayout(new GridLayout(eventlength + 1, 2));

for(int i = 0; i < eventlength; i++) {

JTextField t = new JTextField();

tsetEditable(false);

cadd(new JLabel(event[i], JLabelRIGHT));

cadd(t);

hput(event[i], t);

}

cadd(b1);

cadd(b2);

}

public static void main(String[] args) {

Consolerun(new TrackEvent(), 700, 500);

}

}

#include<iostream>

#include<string>

#include<map>

usingnamespacestd;

intmain()

{

multimap<string,int>m_map;

strings("中国"),s1("美国");

m_mapinsert(make_pair(s,50));

m_mapinsert(make_pair(s,55));

m_mapinsert(make_pair(s,60));

m_mapinsert(make_pair(s1,30));

m_mapinsert(make_pair(s1,20));

m_mapinsert(make_pair(s1,10));

//方式1

intk;

multimap<string,int>::iteratorm;

m=m_mapfind(s);

for(k=0;k!=m_mapcount(s);k++,m++)

cout<<m->first<<"--"<<m->second<<endl;

//方式2

multimap<string,int>::iteratorbeg,end;

beg=m_maplower_bound(s1);

end=m_mapupper_bound(s1);

for(m=beg;m!=end;m++)

cout<<m->first<<"--"<<m->second<<endl;

//方式3

beg=m_mapequal_range(s)first;

end=m_mapequal_range(s)second;

for(m=beg;m!=end;m++)

cout<<m->first<<"--"<<m->second<<endl;

return0;

}

扩展资料

map构造函数;

map<string,int>mapstring;map<int,string>mapint;

map<sring,char>mapstring;map<char,string>mapchar;

map<char,int>mapchar;map<int,char>mapint;

如在打枚举中打印“指定值对应的字符串”时,可是采用map<int,string>的STL实现。

HashMap和ArrayList结合起来用,向HashMap中存值时 把name存入ArrayList中:

HashMap a = new HashMap();

ArrayList al = new ArrayList();

aput("name1", "abcdef"); // key是name,value是字符串abcdef

aladd("name1");

aput("name2","me");

aladd("name2");

aput("name3","you");

aladd("name3");

aput("name4","he");

aladd("name4");

for(int i=0;i<alsize();i++){

Systemoutprintln(aget(alget(i)));

}

以上就是关于map存的是类对象怎么取对象的值看代码全部的内容,包括:map存的是类对象怎么取对象的值看代码、用el表达式怎么取 hashmap的值、HashMap基本用法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9669494.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存