zoukankan      html  css  js  c++  java
  • 数组和集合

    数组

    一维数组

    1.数组定义

    type[] arrayName    或type arrayName []

    int [] arr  或是 int arr2 []

    2.静态初始化

    type arrayName = new type[]{element1,element2,element3}

      例子:int arr []=new int []{1,2,3};

      //普通的数组遍历方式

           for(int i=0 ;i<arr.length;i++){

              system.out.println(arr[i])

             }

     // foreach方式

       for(int z:arr){

      system.out.println( z );

    }

    数组如何取出来,就是用 arr[i]即可

    3.动态初始化

    arrayName =new type[length]

    int arr2[]=new int[3]; 

    arr[0]=1 ;//给数组元素赋值,

    int类型默认的数值为0; 

    二维数组

    静态初始化

    arrayName =new type[]{{element1,element2},{element1,element2},{element1,element2}}

    例子:int[][] arr =new int[][]{{1,2,3},{1,2,3},{1,2,3}}

    动态初始化

    arrayName=new type[length][length]

    例子:int[][] arr =new  int[3][3];

             arr[1][2]=3//赋值

    例子:int arr[][] =new int[][]{{1,2,3},{4,5,6,9},{6,7,8}}

               for(int i=0;i<arr.length;i++){

                for(int j<0;j<arr[i].length;j++){

                system.out.print(arr[i][j]       }

                    }

     

     

    数组排序例子——————起泡法

    对4 ,21,0 ,-12 ,-3排序升序

     

    int arr [ ] ={4,21,-3,-12,0}

    int temp;

    for(int i=0;i<arr.length;i++){

    for(int j=0;j<arr.length-1-i,j++){

           if(arr[j]>arr[j+1]){

           temp=arr[j];

            arr[j+1]=arr[j]

               }

         }

    }

    for(int a:arr){

    system.out.println(a)

    }

    二   集合

    1.用数组存储

    Student students[]  =new Student[3];

    student[0] =new Student("张三",10);

    student[1] =new Student("李四",10);

    student[2] =new Student("王五",10);

    2.List<E>集合

    List是collection接口的子接口。List集合里的元素是可以重复的

    List接口的主要实现类有Arraylist 和Linkedlist

    2.1Arraylist<E>

    ArrayList<String> arraylist =new ArrayList<String>();

    arraylist.add("张三");

    arraylist.add("李四");

    //将指定元素插入到列表中的指定位置 例子:将王五插到第二个位置

    arraylist.add(1,"王五");

    get(int index) :返回此列表中指定位置上的元素

    //将指定的元素替代此列表中指定位置元素

    arraylist.set(1,"小王五");

    2.2 Linkedlist<E>

     LinkedList<String >  linkedlist= new LinkedList<String > ();

    linkedlist.add("张三");

    linkedlist.add("李四");

    linkedlist.add("李五");

    其特有的方法有:

    indexof() 返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回-1

    如 linkedlist.indexof("李四");

    peekFirst()获取但是不移除列表中的第一个元素,如果此列表为空,则返回NULL

    peekLast()获取但是不移除列表中的最后一个元素,如果此列表为空,则返回NULL

     

     3.遍历

     LinkedList<Student >  list= new LinkedList<Student  > ();

    list.add(new Student("张三",10));

    list.add(new Student("李四",10))

    list.add(new Student("王五",10))

     

       使用迭代器遍历  iterator

        Iterator<Student> it=list.iterator()

         while(it.hasNext()){

           Student s=it.next();

             System.out.println(s);

          }

           使用for遍历

       for( student s :list ){ 

           System.out.println(s);

    }

    4.set集合是collection接口

    Hashset   重要特点:1.不允许存在重复的值  2.无序的

    HashSet<String>  hs =new  HashSet<String> ()

    hs.add("232");

    hs.add("25");

    hs.add("22");

    Iterator<String> it =hs.iterator;

    while(it.hasNext()){

    String s=it.next();

    System.out.printIn(s);

    }

     

    5.  map<k,v>

    HashMap<String,Student>   hashMap =new Hash<String,Student>();

    hashMap .put("1号",new student("张三",10));

    hashMap .put("2号",new student("王五",10));

    hashMap .put("3号",new student("李四",10));

    Student s =hashMap.get("1号");

    //获取Key的集合,再获取迭代器

    Iterator<String> it=hashMap.keySet().iterator()

    whlie(it.hasNext()){

    String key=it.next();    //获取key

    student student =hashMap.get(key)     //获取值

    System.out.printIn(s)

    )

    6.list  map   set区别

    1.list列表是顺序存放的,可以有相同的对象,通过索引存取。

    2.set集合是无序存放的,不能有重复的对象,集合无索引,只能通过遍历存取。

    3.map:存放的是键和值的映射,其中键是唯一的值,可以有重复的对象

    三者联系和区别:三者都是接口。list和set都是单列元素的集合。list和set都是继承了collection.而map不是

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    unbuntu系统下在文件中按上下左右出现错误码
    构建之法阅读笔记
    cloudera卸载
    第二阶段团队站立会议04
    第二阶段团队站立会议03
    第二阶段团队站立会议02
    第二阶段团队站立会议01
    团队测试
    cnblogs.com的用户体验
    对于每个小组的意见
  • 原文地址:https://www.cnblogs.com/libaowen609/p/12819731.html
Copyright © 2011-2022 走看看