数组

数组

 用来存放一组数据的数据结构

1.创建数组(3种方法)

l  int[] a = new int[5];

n  新建int[]数组,长度5

n  5个位置存默认值0

n  数组的地址,存到变量a

l  int[] a = {5,3,7,8,6,3,1,6};

l  为一个存在的数组变量赋值,用大括号直接初始化数组

a = new int[]{5,3,7,3};

 

数组的长度属性

(1)A.length

(2)数组一旦创建长度不可变

(3)允许0长度的数组

 

数组的遍历

 从头到尾,依次访问数组的每个位置

for(int i=0; i<a.length; i++) {

   a[i]

}

package day0405;

 

import java.util.Arrays;

 

public class Test1 {

    public static void main(String[] args) {

        f1();

        f2();

        f3();

    }

 

    private static void f3() {

        int[] ints = new int[10];

        for (int i =0;i<ints.length;i++)

        {

            ints[i] = i;

        }

        System.out.println(Arrays.toString(ints));

    }

 

    private static void f2() {

        int[] a = {1,2,3,4,5,6};

        System.out.println(Arrays.toString(a));

 

        a = new int[]{6,5,4,3,2,1};

        System.out.println(Arrays.toString(a));

    }

 

    private static void f1() {

        char[] a = new char[5];

        a[0] = 'h';

        a[1] = 'e';

        a[2] = 'l';

        a[3] = 'l';

        a[4] = 'o';

        System.out.println(Arrays.toString(a));

    }

}

商品录入查询

 

package day0406;

 

import java.util.Scanner;

 

public class Test1 {

 

    static String[] names = {"商品1","商品2","商品3","商品4","商品5"};

    static Double[] price = {10.55,20.66,30.77,40.88,50.99};

    static int[] nums = {1,2,3,4,5};

 

    public static void main(String[] args) {

        outer:

        while(true)

        {

            menu();

            int i = new Scanner(System.in).nextInt();

            switch (i)

            {

                case 1: f1();break;

                case 2: f2();break;

                case 3: f3();break;

                case 4: f4();break;

                case 5: break outer;

            }

        }

    }

 

    private static void menu()

    {

        System.out.println("1:商品列表");

        System.out.println("2:商品录入");

        System.out.println("3:商品查询");

        System.out.println("4:统计信息");

        System.out.println("5:退出");

        System.out.print("选择:");

    }

 

    private static void f4()

    {

        double totalPrice = 0;

        double allPrice = 0;

        double maxTotalPrice = 0;

        double maxPrice = 0;

 

        for (int i=0;i<names.length;i++)

        {

            totalPrice += price[i]*nums[i];

            allPrice += price[i];

            if(price[i] > maxPrice) maxPrice = price[i];

            if(price[i]*nums[i] > maxTotalPrice) maxTotalPrice = price[i]*nums[i];

        }

        System.out.println("总价:"+totalPrice);

        System.out.println("单价均价:"+allPrice/names.length);

        System.out.println("最高总价:"+maxTotalPrice);

        System.out.println("最高单价:"+maxPrice);

    }

 

    private static void f3()

    {

        System.out.print("输入要查询的商品名:");

        String s = new Scanner(System.in).nextLine();

        for (int i=0;i<names.length;i++)

        {

            if(s.equals(names[i]))

            {

                String n = names[i];

                double p = price[i];

                int b = nums[i];

                System.out.println((i+1)+":名称:"+n+",价格:"+p+",数量:"+b);

                return;

            }

        }

        System.out.println("找不到商品");

    }

 

    private static void f2()

    {

        for (int i=0;i<names.length;i++)

        {

            System.out.println("录入第"+(i+1)+"件商品:");

            System.out.print("名称:");

            names[i] = new Scanner(System.in).nextLine();

            System.out.print("价格:");

            price[i] = new Scanner(System.in).nextDouble();

            System.out.print("数量:");

            nums[i] = new Scanner(System.in).nextInt();

        }

        f1();

    }

 

    private static void f1()

    {

        for(int i=0;i<names.length;i++)

        {

            String n = names[i];

            double p = price[i];

            int b = nums[i];

            System.out.println((i+1)+":名称:"+n+",价格:"+p+",数量:"+b);

        }

    }

}