java map 和 String 的使用

前言

此处是简单的map和String的使用方法,记录下来是因为Python和java写串了,因此记录下来提个醒;

 

 

String 的遍历

代码1

 1 public class TestString {
 2     static String s = "hello world";
 3 
 4     static void test03(){
 5         for (int i=0; i<s.length(); i++){
 6             System.out.println(s.charAt(i));
 7         }
 8     }
 9 
10     public static void main(String[] args) {
11         test03();
12     }
13 }

可以正确的遍历String。

 

代码2

 1 public class TestString {
 2     static String s = "hello world";
 3 
 4     static void test02(){
 5         for (int i=0; i<s.length(); i++){
 6             System.out.println(s[i]);
 7         }
 8     }
 9 
10     public static void main(String[] args) {
11         test02();
12     }
13 }

代码报错

java: 需要数组, 但找到java.lang.String

因为不能使用 str[i] 访问字符串中的字符。该操作仅适用于数组。这是Python的用法,我写串了。

 

代码3

 1 public class TestString {
 2     static String s = "hello world";
 3 
 4     static void test01(){
 5         for(Character i:s){
 6             System.out.println(i);
 7         }
 8     }
 9 
10     public static void main(String[] args) {
11         test01();
12     }
13 }

报错

java: for-each 不适用于表达式类型
要求: 数组或 java.lang.Iterable
找到: java.lang.String

增强for循环遍历数组时使用的普通for循环,而遍历集合时使用的Iterator迭代器。

Collection接口继承Iterable,所以Collection的所有子类也实现了Iterable接口。

 
 

Map

 1 public class TestString {
 2     static String s = "hello world";
 3 
 4     static void test01(){
 5         Map<String, Integer> mp = new HashMap();
 6         for(int i =0; i<s.length(); i++){
 7             if (mp.containsKey(s.charAt(i))){
 8                 mp[s.charAt(i)] ++;
 9             }else{
10                 mp[s.charAt(i)] = 1;
11             }
12         }
13     }
14 
15     public static void main(String[] args) {
16         test01();
17     }
18 }

java: 需要数组, 但找到java.util.Map<java.lang.String,java.lang.Integer>

java中map不能像Python中那样直接修改,正确代码如下:

 1 public class TestString {
 2     static String s = "hello world";
 3 
 4     static void test01(){
 5         Map<Character, Integer> mp = new HashMap();
 6         for(int i =0; i<s.length(); i++){
 7             mp.put(s.charAt(i), mp.getOrDefault(s.charAt(i), 1));
 8         }
 9     }
10 
11     public static void main(String[] args) {
12         test01();
13     }
14 }

 

 

map的遍历

 1 public class mapTest {
 2     public static void main(String[] args) {
 3         Map<String, String> map = new HashMap<String, String>();
 4         map.put("student1", "阿伟");
 5         map.put("student2", "小李");
 6         map.put("student3", "小张");
 7         map.put("student4", "小王");
 8 //
 9 //        //1.使用entrySet()遍历
10         System.out.println("使用entrySet()遍历");
11         Iterator it = map.entrySet().iterator();
12         while (it.hasNext()) {
13             Map.Entry entry = (Map.Entry) it.next();
14             Object key = entry.getKey();
15             Object value = entry.getValue();
16             System.out.println("key=" + key + "  value" + value);
17 
18         }
19         //2.通过Map.Keyset遍历key和value,普遍使用,二次取值
20         System.out.println("通过Map.Keyset遍历key和value,普遍使用,二次取值");
21         for (String key : map.keySet()) {
22             System.out.println("Key=" + key + "\tvalue=" + map.get(key));
23         }
24         //3通过map.values()遍历所有的value,但不能遍历key
25         System.out.println("通过map.values()遍历所有的value,但不能遍历key");
26         for (String v : map.values()) {
27             System.out.println("value=" + v);
28         }
29         //4通过map.entrySet遍历key和value(推荐使用,特别是容量大时)
30         System.out.println("通过map.entrySet遍历key和value(推荐使用,特别是容量大时)");
31         for (Map.Entry<String, String> entry : map.entrySet()) {
32             System.out.println("key=" + entry.getKey() + "\tvalue=" + entry.getValue());
33         }
34     }
35 }

 

posted @ 2022-02-13 20:59  r1-12king  阅读(645)  评论(0编辑  收藏  举报