精华内容
下载资源
问答
  • 看到好的转来留着,随时学习。 目录: 声明数组;初始化数组;查看数组长度;遍历数组;int数组转成string数组;...查询数组中的最大值最小值;list的交集并集合集 备注:文内代码具

    看到好的转来留着,随时学习。

    目录:

    1. 声明数组;
    2. 初始化数组;
    3. 查看数组长度;
    4. 遍历数组;
    5. int数组转成string数组;
    6. 从array中创建arraylist;
    7. 数组中是否包含某一个值;
    8. 将数组转成set集合;
    9. 将数组转成list集合;
    10. Arrays.fill()填充数组;
    11. 数组排序;
    12. 复制数组;
    13. 比较两个数组;
    14. 去重复;
    15. 查询数组中的最大值和最小值;
    16. list的交集并集合集

    备注:文内代码具有关联性。

    1.声明数组;

    1
    2
    3
    4
    String [] arr;
    int arr1[];
    String[] array=new String[5];
    int score[]=new int[3];

     

    2.初始化数组;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //静态初始化
    int arr2[]=new int[]{1,2,3,4,5};
    String[] array1={"马超","马云","关羽","刘备","张飞"};
    String[] array2=new String[]{"黄渤","张艺兴","孙红雷","小猪","牙哥","黄磊"};
    int score[]=new int[3];
    //动态初始化
    for(int i=0;i<score.length;i++)
    {   
      score[i]=i+1
    }

     

     

    3.查看数组长度;

    1
    2
    int length=array1.length;
    System.out.println("length:  "+array1.length);

     

    4.遍历数组;

    1
    2
    3
    for (int i = 0; i < array1.length; i++) {
        System.out.println(array1[i]);
    }

     

    5.int数组转成string数组;

    1
    2
    3
    int[]  array3={1,2,3,4,5,6,7,8,9,0};
            String arrStrings=Arrays.toString(array3);
            System.out.println(arrStrings);

     

     6.从array中创建arraylist;

    1
    2
    ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(array1));
    System.out.println(arrayList);

     

    7.数组中是否包含某一个值;

    1
    2
    3
    4
    5
    String a="马超";
    String[] array1={"马超","马云","关羽","刘备","张飞"};
    if (Arrays.asList(array1).contains(a)) {
        System.out.println("马超在这里");
    }

     

    8.将数组转成set集合;

    1
    2
    3
    String[] array2=new String[]{"黄渤","张艺兴","孙红雷","小猪","牙哥","黄磊"};
        Set<String> set=new HashSet<String>(Arrays.asList(array2));
        System.out.println(set);

     

    9.将数组转成list集合;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    String[] array2=new String[]{"黄渤","张艺兴","孙红雷","小猪","牙哥","黄磊"};
    //方法 1.
    List<String> list=new ArrayList<String>();
    for (int i = 0; i < array2.length; i++) {
        list.add(array2[i]);
    }
     
    String[] arrStrings2={"1","2","3"};
    //方法 2.
    List<String > list2=java.util.Arrays.asList(arrStrings2);
    System.out.println(list2);

     

    10.Arrays.fill()填充数组;

    1
    2
    3
    4
    5
    6
    int[] arr3=new int[5];
            Arrays.fill(arr3, 10);  //将数组全部填充10
                   //遍历输出
            for (int i = 0; i < arr3.length; i++) {
                System.out.println(arr3[i]);
            }

     

    11.数组排序;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    //方法 1.
    int[] arr4 = {37219};
    Arrays.sort(arr4);         //.sort(int[] a)   放入数组名字
    for (int i = 0; i < arr4.length; i++) {
        System.out.println(arr4[i]);
    }
    //方法 2.        
    int[] arr5 = {37219,3,45,7,8,8,3,2,65,34,5};
    Arrays.sort(arr5, 14);  //.sort(a, fromIndex, toIndex)    从第几个到第几个之间的进行排序
    for (int i = 0; i < arr5.length; i++) {
            System.out.println(arr5[i]);
    }

     

    12.复制数组;

    1
    2
    3
    4
    5
    6
    7
    8
    //方法 1.
    int[] arr6 = {3721};
    int[] arr7=Arrays.copyOf(arr6, 10);  //指定新数组的长度
    //方法 2.        
    int[] arr8=Arrays.copyOfRange(arr6, 13); //只复制从索引[1]到索引[3]之间的元素(不包括索引[3]的元素)
    for (int i = 0; i < arr8.length; i++) {
            System.out.println(arr8[i]);
    }

     

    13.比较两个数组;

    1
    2
    3
    int[] arr9 = {1234,5,6,7,8,9,0};
            boolean arr10=Arrays.equals(arr6, arr9);
            System.out.println(arr10);

     

    14.去重复;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    int[] arr11 = {1234,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
    //利用set的特性
    Set<Integer> set2=new HashSet<Integer>();
    for (int i = 0; i < arr11.length; i++) {
        set2.add(arr11[i]);
    }
    System.out.println(set2);
    int[] arr12 = new int[set2.size()];
    int j=0;
    for (Integer i:set2) {
        arr12[j++]=i;
    }
    System.out.println(Arrays.toString(arr12));

     

    15.查询数组中的最大值和最小值;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    int[] arr11 = {10234,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
     //计算最大值
    int max = arr11[0];
    for (int i = 1; i < arr11.length; i++) {
         if (arr11[i] > max) {
             max = arr11[i];
        }
      }
      System.out.println("Max is " + max);
      //计算最小值
      int min = arr11[0];
      for (int i = 0; i < arr11.length; i++) {
        if (arr11[i]<min) {
            min = arr11[i];
        }
    }
      System.out.println("Min is " + min);

     

    附:完整代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    package MyTest01;
      
    import java.util.*;
      
    public class ArrayTest02 {
      
        public static void main(String[] args) {
            //声明数组
            String [] arr;
            int arr1[];
              
            //初始化数组
            int arr2[]=new int[]{1,2,3,4,5};
            String[] array1={"马超","马云","关羽","刘备","张飞"};
            String[] array2=new String[]{"黄渤","张艺兴","孙红雷","小猪","牙哥","黄磊"};
              
            String[] array=new String[5];
          
              
            //查看数组的长度
            int length=array1.length;
            System.out.println("length:  "+array1.length);
              
            //输出数组
    //      System.out.println(array1);     //结果:[Ljava.lang.String;@32f22097
            System.out.println("arr2:  "+Arrays.toString(arr2));
              
            //遍历数组
            for (int i = 0; i < array1.length; i++) {
    //          System.out.println(array1[i]);
            }
            //int数组转成string数组
            int[]  array3={1,2,3,4,5,6,7,8,9,0};
            String arrStrings=Arrays.toString(array3);
    //      System.out.println(arrStrings);
              
            //从array中创建arraylist
            ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(array1));
            System.out.println(arrayList);
              
            //数组中是否包含某一个值
            String a="马超";
            if (Arrays.asList(array1).contains(a)) {
                System.out.println("马超在这里");
            }
              
            //将数组转成set集合
            Set<String> set=new HashSet<String>(Arrays.asList(array2));
            System.out.println(set);
              
            //将数组转成list集合
            List<String> list=new ArrayList<String>();
            for (int i = 0; i < array2.length; i++) {
                list.add(array2[i]);
            }
            String[] arrStrings2={"1","2","3"};
            List<String > list2=java.util.Arrays.asList(arrStrings2);
            System.out.println(list2);
              
              
            //Arrays.fill()填充数组
            int[] arr3=new int[5];
            Arrays.fill(arr3, 10);  //将数组全部填充10
            for (int i = 0; i < arr3.length; i++) {
                System.out.println(arr3[i]);
            }
              
            //数组排序
            int[] arr4 = {37219};
            Arrays.sort(arr4);
            for (int i = 0; i < arr4.length; i++) {
                System.out.println(arr4[i]);
            }
              
            int[] arr5 = {37219,3,45,7,8,8,3,2,65,34,5};
            Arrays.sort(arr5, 14);  //从第几个到第几个之间的进行排序
            for (int i = 0; i < arr5.length; i++) {
                System.out.println(arr5[i]);
            }
              
              
            //复制数组
            int[] arr6 = {3721};
            int[] arr7=Arrays.copyOf(arr6, 10);  //指定新数组的长度
              
            int[] arr8=Arrays.copyOfRange(arr6, 13); //只复制从索引[1]到索引[3]之间的元素(不包括索引[3]的元素)
            for (int i = 0; i < arr8.length; i++) {
                System.out.println(arr8[i]);
            }
              
            //比较两个数组
            int[] arr9 = {1234,5,6,7,8,9,0};
            boolean arr10=Arrays.equals(arr6, arr9);
            System.out.println(arr10);
          
            //去重复
            //利用set的特性
            int[] arr11 = {1234,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
            Set<Integer> set2=new HashSet<Integer>();
            for (int i = 0; i < arr11.length; i++) {
                set2.add(arr11[i]);
            }
                System.out.println(set2);
            int[] arr12 = new int[set2.size()];
            int j=0;
            for (Integer i:set2) {
                arr12[j++]=i;
            }
            System.out.println(Arrays.toString(arr12));
        }
      
    }

     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    package MyTest01;
     
    public class ArrayTest03 {
     
        public static void main(String[] args) {
             
            int[] arr11 = {10234,5,6,7,8,9,0,3,2,4,5,6,7,4,32,2,1,1,4,6,3};
             //计算最大值
            int max = arr11[0];
            for (int i = 1