数组

作者: 荐航 | 来源:发表于2018-02-24 16:31 被阅读0次

    定义

    • 声明一个变量就是在内存空间划出一块合适的空间
    • 声明一个数组就是在内存空间划出一串连续的空间

    基本要素

    • 标识符
    • 数组元素
    • 元素下标:一般从零开始
    • 元素类型

    使用数组步骤

    注意

    • 数组长度固定不变,避免数组越界
    • 数组中的所有元素必须属于相同的数据类型

    例题

    • 1-10数字之和
    int count[]=new int[10];
            int i=0;
            for ( i=0;i<10;i++)
            {
                count[i]=i+1;
            }
            int total=0;
            for(i=0;i<10;i++)
            {
                total=total+count[i];
            }
            System.out.println(total);
    
    • 边声明边赋值
      -- int a[]={23,24,25}
      -- int a[]=new int[]{23,24,25}
      -- 动态的在键盘录入信息并赋值
    Scanner sc=new Scanner(System.in);
    for(int i=1;i<=10;i++)
    {
    int score[i]=sc.nextInt();
    }
    
    • 数组排序:Arrays.sort();[对数组进行升序排序]
    • 循环录入五位学员成绩,进行升序排序输出结果
    Scanner sc=new Scanner(System.in);
            int count[]=new int[5];
    
            for(int i=0;i<5;i++)
            {
                System.out.println("请输入学员成绩");
                count[i] =sc.nextInt();
            }
            Arrays.sort(count);
            for(int i=0;i<5;i++)
            {
                System.out.println(count[i]);
            }
    
    • 数组插入算法
    • 有一组学员的成绩{99,85,82,63,60},将他们按升序排列,要增加一个学员的成绩,将它插入成绩序列,并保持升序。
    int scores[]=new int[]{99,85,82,63,60};
            Arrays.sort(scores);
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入成绩");
            int score=sc.nextInt();
            int score3[]=new int[6];
            int pos=0;
            for(int i=0;i<5;i++)
            {
                if (score > scores[i]) {
                    pos = i;
                }
            }
                score3[pos]=score;
                for(int i=0;i<pos;i++)
                {
                    score3[i]=scores[i];
                }
                for(int i=pos+1;i<6;i++)
                {
                    score3[i]=scores[i-1];
                }
                for(int i=0;i<6;i++)
                {
                    System.out.println(score3[i]);
                }
    
    • 字符逆序输出
      将a,c,u,b,e,p,f,z进行升序和逆序输出
     char word[]=new char[]{'a','c','u','b','e','p','f','z'};
            Arrays.sort(word);
            System.out.println(word);
            for(int i=word.length-1;i>=0;i--)
            {
                System.out.println(word[i]);
            }
    

    字符串数组

    • 定义一个字符串数组,查找某个字符串在数组中出现的次数。
    String[] array={"li","ok","lk","li","ui","liu","li"};
            String name="li";
            int count=0;
            for(int i=0;i<array.length;i++)
            {
                if(array[i].equals(name))
                {
                    count++;
                }
            }
            System.out.println(count);
    
    char[] arr = {'a','c','u','b','p','f','z'};
    System.out.println(arr);
    Arrays.sort(arr);
    System.out.println(arr);
    Scanner sc = new Scanner(System.in);
    System.out.print("待插入的字符是");
    String zifu = sc.next();
    char zf = zifu.charAt(0);
    int pos = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] >= zf) {
            pos = i;
            break;
        }
    }
    System.out.println("插入字符的下标"+pos);
    char[] a = new char[arr.length+1];
    a[pos]=zf;
    
    for(int i = 0;i < pos;i++)
    {
        a[i] = arr[i];
    }
    for(int i = pos;i < a.length-1;i++)
    {
        a[i+1]=arr[i];
    }
    for(int i = 0;i < a.length;i++)
    {
        System.out.print(a[i]+"\t");
    }
    
    
    public static void main(String[] args) {
            char word = 'm';
            char[] charArray2 = {'a', 'c', 'u', 'b', 'e', 'p', 'f', 'z'};
            String str = "acubepfz";
            Arrays.sort(charArray2);
            System.out.println(charArray2);
            char[] arr = new char[9];
            int index = 0;
            for (int i = 0; i < arr.length; i++)
            {
                if (i < 6) {
                    arr[i] = charArray2[i];
                }
                else if (i == 6) {
                    arr[i] = word;
                }
                else if(i>6)
                {
                    arr[i] = charArray2[i-1];
                }
        }
            System.out.println(arr);
                }
            }
    

    相关文章

      网友评论

          本文标题:数组

          本文链接:https://www.haomeiwen.com/subject/vbafgxtx.html