zoukankan      html  css  js  c++  java
  • Java学习笔记之:Java数组

    一、介绍

    数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同。

    Java语言中提供的数组是用来存储固定大小的同类型元素。

    你可以声明一个数组变量,如numbers[100]来代替直接声明100个独立变量number0,number1,....,number99。

    二、笔记

    1.一维数组

    /**
     * 数组:用来表示一种固定长度相同数据类型的组合
     * 
     * @author HuTiger
     *
     */
    public class ArrayStudy {
    
        public static void main(String[] args) {
    
            int[] array = { 1, 2, 3, 4, 5 };// 整型数组 长度是5
    
            String[] strs = { "AA", "BB", "CC" };// 字符串数组 长度是 3
    
            // 数组的声明方式 : 类型和变量名
            int[] array1; // 最常用
            int array2[]; // 偶尔使用
            int[] array3; // 基本不用
    
            // 数组的初始化方式:有两种
            /*
             * 静态初始化:必须在声明的时候进行初始化
             */
            //int[] array = { 1, 2, 3, 4, 5 };
    
            /*
             * 动态初始化:必须与new关键字一起使用,需要指定固定长度
             */
    //        String[] strs = null;
    //        strs=new String[5];
            
    //        String[] strs=new String[5];
            
            /*
             * 动态初始化的时候,数组中的每个元素的值是对应的数据类型的默认值
             */        
    //        int[] array4=new int[5];
    //        System.out.println(array4[1]);
    //        System.out.println(array4[2]);
    //        System.out.println(array4[3]);
    //        
    //        String[] str1=new String[5];
    //        System.out.println(str1[1]);
    //        System.out.println(str1[2]);
    //        System.out.println(str1[3]);
            
            /*
             * byte short int long float double boolean char    引用数据类型
             * 0      0    0   0    0.0   0.0   false    u0000  null     
             */
            
            /*
             * 获取数组中元素的值:可以通过他的下标索引来获取到对应位置的元素的值。下标是从0开始
             * 数组的长度:可以通过array.length 属性获取。ps:长度是属性
             */
            int[] arrays={9,4,5,5,4,4};
            System.out.println(arrays[0]);
            //获取数组的长度
            System.out.println(arrays.length);
        
            //通过数组的下标对数组中对应位置的元素进行赋值
            arrays[1]=10;
            System.out.println(arrays[1]);
            
            String str="adsfvg";
            //获取字符串的长度是通过方法来获取的
            System.out.println(str.length());
            
        
            int[] longarray={8,7,4,2,0,4,8,6,2,0,1,5,0,0,10};
            for (int i = 0; i < longarray.length; i++) {
                System.out.print(longarray[i]+" ");
            }
            System.out.print("
    ");
            /*
             * 使用while循环输出
             * 数组的最大index为 length-1
             */
            int i=0;
            while(i<longarray.length)
            {
                System.out.print(longarray[i]+" ");
                i++;
            }
        }
    }

    2.二维数组

    package com.hgd.study3;
    
    /**
     * 二维数组:
     * 如何创建二维数组
     * 二维数组的遍历
     * @author HuTiger
     *
     */
    public class ErWeiArray {
    
        public static void main(String[] args) {
            
            //静态初始化
            int[][] array={{1,2,3},{1,2,3},{3,2,1},{3,2,1}};
            
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    System.out.print(array[i][j]);
                }
                System.out.println("
    ");
            }
            
            //动态初始化,需要对数组中的每一个元素进行动态初始化
            int[][] arrays=new int[5][];
            System.out.println(arrays.length);
            System.out.println(arrays[0]);
            array[0]=new int[3];
            System.out.println(arrays[0]);
            
        }
    }

    3.数组练习

    package com.hgd.study3;
    
    import java.awt.geom.FlatteningPathIterator;
    
    import javax.swing.text.StyledEditorKit.ForegroundAction;
    
    /**
     * 排序
     * 
     * @author HuTiger
     *
     */
    public class ArrayPratice {
        public static void main(String[] args) {
            
        }
        /*
         * 冒泡排序法
         */
        private static void MaoPao() {
            
            int[] array = { 7, 5, 9, 1, 3, 6 };
            for (int i = 0; i < array.length; i++) {
                for (int j = i + 1; j < array.length; j++) {
                    int tmp = 0;
                    if (array[i] > array[j]) {
                        tmp = array[i];
                        array[i] = array[j];
                        array[j] = tmp;
                    }
                }
            }
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + " ");
            }
        }
    
        /*
         * 求{2,1,6,5,4}最大值最小值平均值
         */
        private static void GetMaxMinAvg() {
            int[] array = { 2, 2, 3, 4, 6 };
            int max = array[0];
            int min = array[0];
            int sum = 0;
            for (int i = 0; i < array.length; i++) {
                sum += array[i];
                if (array[i] > max) {
                    max = array[i];
                }
                if (min > array[i]) {
                    min = array[i];
                }
            }
            System.out.println("最大值:" + max);
            System.out.println("最小值:" + min);
            System.out.println("平均值:" + (float) sum / array.length);
        }
    
        /*
         * 现在有如下一个数组:int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,,7,6,7,0,5};
         * 要求将以上数组中的0去掉,将不为0的值存入一个新的数组,新的数组为:int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}
         */
        private static void RemoveZero() {
            int oldArr[] = { 1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5 };
            int[] tmparry = new int[oldArr.length];
            int[] newArr;
            int k = 0;
            int count = 0;
            for (int i = 0; i < oldArr.length; i++) {
                if (oldArr[i] != 0) {
                    tmparry[k] = oldArr[i];
                    count++;
                    k++;
                }
            }
            newArr = new int[count];
            for (int i = 0; i < newArr.length; i++) {
                newArr[i] = tmparry[i];
            }
            for (int i = 0; i < newArr.length; i++) {
                System.out.print(newArr[i] + " ");
            }
    
        }
    
        /*
         * 去除重复数组元素
         */
        private static void RemoveRepeat() {
            int[] array = { 1, 1, 1, 2, 3, 3, 4 };
            int lengt = 0;
            int count = 1;
            for (int i = 0; i < array.length; i++) {
                int l = i, r = i;
                while (--i >= 0) {
                    if (array[i] == array[l]) {
                        count++;
                        break;
                    }
    
                }
                while (++r <= array.length - 1) {
                    if (array[i] == array[r]) {
                        count++;
                    }
                }
                if (count == 1) {
                    lengt++;
                }
            }
            int[] newArr = new int[lengt];
            int index = 0;
            for (int i = 0; i < array.length; i++) {
                int l = i, r = i;
                while (--i >= 0) {
                    if (array[i] == array[l]) {
                        count++;
                    }
    
                }
                while (++r <= array.length - 1) {
                    if (array[i] == array[r]) {
                        count++;
                    }
                }
                if (count == 1) {
                    lengt++;
                }
            }
        }
    
        private static void RemoveRepeat2() {
            int oldArr[] = { 2, 0, 3, 2, 9, 0 };
            int lengh = 0;
            int a = 0;
            int aIndex = -1;
            int count=0;
            for (int i = 0; i < oldArr.length; i++) {
                if (oldArr[i] == a) {
                    aIndex = i;
                    break;
                }
            }
            for (int i = 0; i < oldArr.length; i++) {
                for (int j = i + 1; j < oldArr.length; j++) {
                    if (oldArr[i] == oldArr[j] && oldArr[j] != a) {
                        oldArr[j] = a;
    
                    }
                }
                if(oldArr[i]==a)
                {
                    count++;
                }
            }
            lengh=aIndex==-1?oldArr.length-count:oldArr.length-count+1;
            int[] newArr=new int[lengh];
            int index=0;
            for (int i = 0; i < newArr.length; i++) {
                if(oldArr[i]!=a||i==aIndex){
                    newArr[index]=oldArr[i];
                    index++;
                    
                }
            }
            
        }
    
    }
  • 相关阅读:
    matlablib安装Helvetica字体
    word插入矢量图的方法
    字体对应关系
    MySQL8.0.15基于mycat读写分离(windows环境)
    2.MySQL8.0.15主从同步(windows环境)
    1.MySQL8.0.15安装步骤(windows环境)
    mysql自带的压力测试工具
    FreeFileSync + Windows任务计划程序 实现自动备份功能
    FreeFileSync 设置服务器文件自动备份,同时可做本地代码程序同步软件
    dokcer镜像添加ssh服务
  • 原文地址:https://www.cnblogs.com/huguodong/p/5872501.html
Copyright © 2011-2022 走看看