程序题分析:将一个字符串数组转换成一个字符串集合

package 程序题;

import java.util.HashMap;
import java.util.Map;

/**
 * 将一个字符串数组转换成一个字符串集合
 * @author 朱方圆
 *
 */
public class T1 {
    
    
	public static void main(String[] args) {
    
    
		
		String[] a = new String[4];//定义一个字符串数组,数组名为a,长度为4
		
		//分别将集合a中存储元素
		a[0] = "a";
		a[1] = "b";
		a[2] = "c";
		a[3] = "d";
		
		Map<Integer, String> c = new HashMap<Integer,String>();//定义一个HashMap集合对象,父类集合指向子类集合,泛型分别为Integer,String类型
		
		String countChar = null;//定义一个变量作为统计值,countChar,初始值为null
		
		for(int i = 0; i < a.length ; i++) {
    
    //循环遍历,集合中的元素
			
			countChar = a[i];//将数组a中的各个元素分别赋值给countChar
			c.put(i,countChar);//集合通过调用通过put()方法,分别将获得的元素存储在集合中
			
		}
		
		for(Map.Entry<Integer,String> entry : c.entrySet()) {
    
    //循环遍历集合中的元素
			
			System.out.println(entry);
			
		}
	}
}

猜你喜欢

转载自blog.csdn.net/zhu_fangyuan/article/details/108292389