entrySet怎么用的。。。

J2EE 码拜 9年前 (2015-04-16) 1186次浏览 0个评论

请求地址:http://localhost:8080/yhrecord/projman/test.jsp?name=sky&age=12

Map<String,String> map=request.getParameterMap();
	Set<Map.Entry<String, String>> entry=map.entrySet();
	for(Map.Entry<String,String> entry_:entry){
		System.out.println(entry_.getKey().toString()+":"+entry_.getValue().toString());
	}

System.out.println(entry_.getKey().toString()+”:”+entry_.getValue().toString());
这行会报:java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String

请问怎么破。。。

entrySet怎么用的。。。
应该是map里面的值有问题,把map里的值打印出看看,或者断点下
entrySet怎么用的。。。
测试输出正常
entrySet怎么用的。。。
貌似request.getParameterMap() 返回的是个Map<String,String[]>
不是Map<String,String>吧
所以强转报错了
entrySet怎么用的。。。
引用 2 楼 Menglinyang 的回复:

貌似request.getParameterMap() 返回的是个Map<String,String[]>
不是Map<String,String>吧
所以强转报错了

不是你说的这样的 

这个是eclipse中提示的api

Set<Entry<String, String>> java.util.Map.entrySet()

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes 
 to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the 
 set is in progress (except through the iterator""s own remove operation, or through the setValue 
 operation on a map entry returned by the iterator) the results of the iteration are undefined. The set 
 supports element removal, which removes the corresponding mapping from the map, via the 
 Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or 
 addAll operations. 
Returns:
	a set view of the mappings contained in this map
entrySet怎么用的。。。
引用 1 楼 shixitong 的回复:

应该是map里面的值有问题,把map里的值打印出看看,或者断点下
entrySet怎么用的。。。
测试输出正常

  

你说的这个跟我的  好像不是一码事。。   我这个是request中的。。

entrySet怎么用的。。。
引用 4 楼 Gemerl 的回复:
Quote: 引用 1 楼 shixitong 的回复:

应该是map里面的值有问题,把map里的值打印出看看,或者断点下
entrySet怎么用的。。。
测试输出正常

  

你说的这个跟我的  好像不是一码事。。   我这个是request中的。。

Map<String,String> map=request.getParameterMap();
改为这个
Map<String,String[]> map = request.getParameterMap();
看request的Api

entrySet怎么用的。。。
getParameterMap
public Map getParameterMap()
The default behavior of this method is to return getParameterMap() on the wrapped request object. 

Specified by:
getParameterMap in interface ServletRequest
Returns:
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.

The values in the parameter map are of type String array.

Map<String, String[]> map = request.getParameterMap();
Set<Map.Entry<String, String[]>> entry = map.entrySet();
entrySet怎么用的。。。
30分
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test17 {

	public static void main(String[] args) {

		Map<String, String[]> map = new HashMap<String,String[]>();
		map.put("B", new String[]{"B","C","D"});
		map.put("A", new String[]{"B","C","D"});
		Set<Map.Entry<String, String[]>> entry = map.entrySet();
		for (Map.Entry<String, String[]> entry_ : entry) {
			System.out.println(entry_.getKey().toString() + ":"
					+ java.util.Arrays.toString(entry_.getValue()));
		}

	}

}
entrySet怎么用的。。。
引用 7 楼 shixitong 的回复:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test17 {

	public static void main(String[] args) {

		Map<String, String[]> map = new HashMap<String,String[]>();
		map.put("B", new String[]{"B","C","D"});
		map.put("A", new String[]{"B","C","D"});
		Set<Map.Entry<String, String[]>> entry = map.entrySet();
		for (Map.Entry<String, String[]> entry_ : entry) {
			System.out.println(entry_.getKey().toString() + ":"
					+ java.util.Arrays.toString(entry_.getValue()));
		}

	}

}

谢谢你 ,@shixitong 。你这个可以  但是我想问   entry_.getValue()   得到的是内存地址   怎么得到真实值。。

entrySet怎么用的。。。
谢谢你 ,@shixitong    
     已经知道了   entry_.getValue()[0]   这个得到的是数组。。
entrySet怎么用的。。。
引用 8 楼 Gemerl 的回复:
Quote: 引用 7 楼 shixitong 的回复:
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Test17 {

	public static void main(String[] args) {

		Map<String, String[]> map = new HashMap<String,String[]>();
		map.put("B", new String[]{"B","C","D"});
		map.put("A", new String[]{"B","C","D"});
		Set<Map.Entry<String, String[]>> entry = map.entrySet();
		for (Map.Entry<String, String[]> entry_ : entry) {
			System.out.println(entry_.getKey().toString() + ":"
					+ java.util.Arrays.toString(entry_.getValue()));
		}

	}

}

谢谢你 ,@shixitong 。你这个可以  但是我想问   entry_.getValue()   得到的是内存地址   怎么得到真实值。。

为什么说是内存地址,依据是什么,java里是得不到内存地址的


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明entrySet怎么用的。。。
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!