数组

作者: 荐航 | 来源:发表于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);
            }
        }

相关文章

  • 数组

    数组数组数组数组数组数组数组数组数组

  • JavaScript - 5.数组<增删改查>

    数组 Array 数组 - 增 数组 - 删 / 改 数组 - 查 数组 - 自动 toString() 数组 -...

  • PHP数组使用

    数组定义 数组增、删、改 数组查询 数组排序 数组合并、分割 数组比较、去重复 数组长度 数组遍历 数组转换 其他...

  • 》》》PHP初入---(三)

    数组定义 1.索引数组:数组下标是整型的 声明数组: 访问数组: count(数组)--获取数组长度 查看数组所有...

  • JavaScript中数组的常用操作

    数组的遍历 数组的映射 数组的简化 数组的连接 获取数组的片段 数组的拷贝 查找数组 数组去重

  • JavaSE之数组

    六、数组 目录:数组概述、数组声明创建、数组使用、多维数组、Array类、稀疏数组 1.什么是数组 数组的定义:数...

  • Shell数组、关联数组

    数组 定义数组 获取数组 关联数组 定义关联数组 获取关联数组

  • 学习Java第五天

    数组是多个数据的集合 数组的语法 数组元素类型【】 数组名; 多维数组: 数组元素类型【】【】 数组名; 多维数组...

  • php基础精粹

    PHP php数组 php数组之索引数组初始化 PHP数组之索引数组赋值 PHP数组之访问索引数组内容 PHP数组...

  • C语言的惯用集

    数组部分 数组部分 清空数组a 把数据读进数组a 对数组a求和

网友评论

      本文标题:数组

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