码迷,mamicode.com
首页 > 编程语言 > 详细

javawb学习01

时间:2018-04-15 13:33:48      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:javawb学习01

增强for循环:

package Day01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Test;

//文件复制
public class Demo1 {

    //
    @Test
    public  void main1() {

        int[] is = {1,2,3,4,5,6};
        for (int i = 0;i < is.length; i++)
        {
            System.out.println(is[i]);
        }

    }

    //遍历内容使用
    @Test  
    public void test1()
    {
        int[] is = {1,2,3,4,5};
        for(int value : is){
            System.out.print(value+"\t");
        }
    }

    //传统方式
    @Test
    public void testSet()
    {
        Set<Integer> set = new HashSet<Integer>();
        set.add(100);
        set.add(200);
        set.add(300);
        Iterator<Integer> it = set.iterator();
        while(it.hasNext()){
            Integer key = it.next();
            System.out.print(key+"\t");
        }
    }

    @Test
    public void testlist()
    {
        List<String> list = new ArrayList<String>();
        list.add("jack");
        list.add("marry");
        list.add("sisi");
        for(String name : list){
            System.out.print(name+"\t");
        }
    }

    //传统方法
    @Test
    public void testMap1(){
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(100,"jack");
        map.put(200,"marry");
        map.put(300,"sisi");
        Set<Integer> set = map.keySet();
        Iterator<Integer> it = set.iterator();
        while(it.hasNext()){
            Integer key = it.next();
            String value = map.get(key);
            System.out.println(key+"-"+value);
        }
    }

    @Test
    public void testMap2(){

        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(100,"jack");
        map.put(200,"marry");
        map.put(300,"sisi");
        Set<Entry<Integer,String>> set = map.entrySet();
        Iterator<Entry<Integer,String>> it = set.iterator();
        while(it.hasNext()){
            Entry<Integer,String> entry = it.next();
            Integer key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"<->"+value);
        }

    }

}

javawb学习01

标签:javawb学习01

原文地址:http://blog.51cto.com/357712148/2103586

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!