jvav数组

数组的概念?有什么特点?

答:数组(array)是一种数据结构其功能是存储相同数据类型变量的值,可以使用共同的名称引用它。数组可以被定义位任何类型,可以以是一维数组或者多维数组,数组是有序觉的集合,数组中的每个元素相同的数据类型,可以用一个的数组名和下标来唯一确定数组中的元素

      特点:

                元素:数组的每个数组称为元素。

                长度:数组中的元素个数,是数组中的一个属性length-1。

                索引:下标,第一个元素的索引为0,最后一个元素的索引为length-1

数组的定义及初始化格式?

   答:数组的定义:

          1.  数组类型[ ]  数组名 = new 数组类型[数组元素的长度]

           int[]   array  =  new  int[5]

          初始化格式:数组的动态初始化

           2.  int[ ]   数组名  = 值

      int[ ]   array ={1314,520,521}

           初始化格式:数组的静态初始化 

思考从键盘录入多名会员信息,并进行存储,最后显示打印出来

package com.hpf;

import java.util.Scanner;

public class Arrays {
    public static void main(String[] args) {
        System.out.println("请输入你要录入会员的数目:");
        Scanner sc = new Scanner(System.in);
        int n =sc.nextInt();
        Hy[] a = new Hy[n];
        for (int i = 0; i < n; i++) {
            a [i] = new Hy();
            System.out.println("请输入第"+(i+1)+"位会员信息:");
            a [i].The();
        }
        for (int i = 0; i < n; i++) {
            System.out.println("输出第"+(i+1)+"位会员信息:");
            a [i].ShuChu();
        }
    }
    static class Hy{
        //姓名
        String name;
        //积分
        float jf;
        public void The(){
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的会员姓名:");
            name = sc.next();
            System.out.println("请输入你的会员积分:");
            jf = sc.nextFloat();
        }
        public void ShuChu(){
            System.out.println("会员信息");
            System.out.println("你的会员姓名:"+name);
            System.out.println("你的会员积分:"+jf);
        }
    }
}

 对数组的元素进行反转。

public class Shu {
    public static void main(String[] args) {
       int[] array = new int[5];
        for (int i = 0; i < array.length; i++) {
            //键盘扫描器
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数组第"+(i+1)+"个整数:");
            int ac =sc.nextInt();
            array[i] = ac;
            System.out.print("元素"+array[i]+"\t");
        }
        System.out.println();
        System.out.println(Arrays.toString(array));
        for (int i = 0,end=array.length-1; i <= end; i++,end--) {
            int temp = array[i];
            array[i] = array[end];
            array[end] = temp;
        }
        System.out.println("对数组的元素反转:"+Arrays.toString(array));

    }
  }

找出数组的最大值和最小值。

public class Shu {
    public static void main(String[] args) {
       int[] array = new int[5];
        for (int i = 0; i < array.length; i++) {
            //键盘扫描器
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入数组第"+(i+1)+"个整数:");
            int ac =sc.nextInt();
            array[i] = ac;
            System.out.print("元素"+array[i]+"\t");
        }
        System.out.println();
        int max = array[0]; //最大值的容器
        int man =array[0];  //最小值的容器
        for (int i = 1; i < array.length; i++) {
              if (max<array[i]){  //比max大的进行if里的循环体,否则退出循环。
                  max=array[i];
              }
              if (man>array[i]){  //比man小的进行if里的循环体,反之退出循环。
                  man =array[i];
              }
        }
        System.out.println("最大值是:"+max);
        System.out.println("最小值是:"+man);

    }
  }

对二维数组的每个元素打印在控制台。

public class Shu {
    public static void main(String[] args) {
      //打印输出二维数组int[][] arr={{6,0,8,9},{8,9,6},{9,8},{9}}的每个元素到控制台
        int[][] arr ={{6,0,8,9},{8,9,6},{9,8},{9}};
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }

    }
  }

求二维数组四季公司的销售额的总和。

public class Shu {
    public static void main(String[] args) {
       /**
         * 公司年销售额求和(扩展题)
         * 	某公司按照季度和月份统计的数据如下:单位(万元)
         * 	第一季度:22,66,44
         * 	第二季度:77,33,88
         * 	第三季度:25,45,65
         * 	第四季度:11,66,99
         * */
        float cont = 0;//求和
        float[][] arr1 ={{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                cont += arr1[i][j];
            }
        }
        System.out.println("销售额和是:"+cont);

    }
  }


给自己一个舞台,让你的人生更精彩!

                   作者:渣男あ(普通人) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

渣男あ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值