java---数组

4.1 声明一个整型数组,大小为10,循环依次赋值为1的平方,2的平方....一直到10的平方

  4.2 计算该数组中所有元素的平均值

  5.3 计算该数组中偶数之和减去奇数之和的结果 

package com.Arry;


/**
 * 4.1 声明一个整型数组,大小为10,循环依次赋值为1的平方,2的平方....一直到10的平方
 * 4.2 计算该数组中所有元素的平均值
 * 5.3 计算该数组中偶数之和减去奇数之和的结果
 */
public class Demo01 {
    public static void main(String[] args) {
        int[] arr = new int[10];
        double avg = 0;
        double sum = 0;
        int sum1 = 0;
        int sum2 = 0;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (i + 1) * (i + 1);
            sum += arr[i];
            if (arr[i] % 2 == 0) {
                sum2 += arr[i];
            } else {
                sum1 += arr[i];
            }
            System.out.print(arr[i]+ " ");
        }
        avg = sum / arr.length;
        System.out.println("该数组中所有的元素的平均值为:" + avg);
        int sum3 = sum2 - sum1;
        System.out.println("数组中偶数之和减去奇数之和的值为:" + sum3);
    }
}

1,从键盘输入10个整数,存储到数组中; 计算输入的值(来自数组)中所有的偶数之和

package com.Arry;

import java.util.Scanner;

/**
 *   1,从键盘输入10个整数,存储到数组中; 计算输入的值(来自数组)中所有的偶数之和
 */
public class Demo02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[10];
        int sum = 0;
        for (int i = 0; i < 10; i++) {
            System.out.println("请输入第" + (i + 1) + "个数");
            int a = sc.nextInt();
            arr[i] = a;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
            if (arr[i] % 2 == 0) {
                sum += arr[i];
            }
        }
        System.out.println("该数组中所有偶数之和:"+sum);
    }
}


   2,判断一个指定的整数是否存在于某个整型数组中

package com.Arry;

/**
 * 2,判断一个指定的整数是否存在于某个整型数组中
 */
public class Demo03 {
    public static void main(String[] args) {
        int a = 1000;
        int[] arr = {1, 2, 3, 4, 65, 67, 7, 8, 54, 3, 122, 354, 65, 10};
        boolean b = false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == a) {
                b = true;
            }
        }
        if (b==true){
            System.out.println("该指定整数在数组中存在");
        }else {
            System.out.println("不存在!");
        }
    }
}


   3,将一个整型数组中所有的奇数存储到另一个整型数组中

package com.Arry;

/**
 * 将一个整型数组中所有的奇数存储到另一个整型数组中
 */
public class Demo04 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 2, 3, 4, 5};
        int[] b = new int[10];
        int m = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] % 2 != 0) {
                b[m] = a[i];
                m++;
            }
        }
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i]+" ");
        }
    }
}


   4,将数组倒置,如{1,2,3,4,5}  变为 {5,4,3,2,1}

package com.Arry;

/**
 * 4,将数组倒置,如{1,2,3,4,5}  变为 {5,4,3,2,1}
 */
public class Demo05 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        int[] b = new int[a.length];
        int n = a.length - 1;
        for (int i = 0; i < a.length; i++) {
            b[n] = a[i];
            n--;
        }
        for (int i = 0; i < b.length; i++){
            System.out.print(b[i]+" ");
        }
    }
}


   5,判断两个整型数组是否完全相等(长度和对应位置的元素都要相等)

package com.Arry;

import java.util.logging.Level;

/**
 * 5,判断两个整型数组是否完全相等(长度和对应位置的元素都要相等)
 */
public class Demo06 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        int[] b = {3, 2, 5, 1, 4};
        int c = 0; //作为计数器
        if (a.length == b.length) {
            for (int i = 0; i < a.length; i++) {
                if (a[i] == b[i]) {
                    c++;
                }
            }
            if (c == a.length) {
                System.out.println("数组相同");
            } else {
                System.out.println("数组不同");
            }
        } else {
            System.out.println("数组不同");
        }
    }
}


   6,将一个数组的奇数索引位拷贝到另一个数组中前半部分,偶数索引位数字拷贝到后半部分
       {1,2,3,4,5}   =  {2,4,1,3,5}

package com.Arry;

/**
 * 将一个数组的奇数索引位拷贝到另一个数组中前半部分,偶数索引位数字拷贝到后半部分
 *        {1,2,3,4,5}   =  {2,4,1,3,5}
 */
public class Demo07 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        int[] b = new int[a.length];
        int n = a.length - 1;
        int c = 0;
        for (int i = 0; i < a.length; i++) {
            if (i % 2 != 0) {
                b[c] = a[i];
                c++;
            }
        }
        for (int i = a.length - 1; i >= 0; i--) {
            if (i % 2 == 0) {
                b[n] = a[i];
                n--;
            }
        }
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i] + " ");
        }

    }
}


   7,随机产生1000个1-10的随机数,统计它们出现的次数  (利用Math.random()产生随机数)  (技巧)

package com.Arry;

import java.util.Random;

/**
 *  7,随机产生1000个1-10的随机数,统计它们出现的次数  (利用Math.random()产生随机数)  (技巧)
 */
public class Demo08 {
    public static void main(String[] args) {

        int[] a = new int[1000];
        int[] b = new int[10];
        Random random = new Random(1);
        for (int i = 0; i < 1000; ++i) {
            a[i] = (int)(Math.random()*10);
        }
        for (int i = 0; i < 1000; ++i){
            b[a[i]]++;
        }
        //输出
        for(int i=0 ; i<b.length ; ++i)
        {
            System.out.println(i+"出现了:"+b[i]+" 次");
        }
    }
}


   8,获取一个整型数组中最大的值(打擂台)

package com.Arry;

/**
 * 8,获取一个整型数组中最大的值(打擂台)
 */
public class Demo09 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, -15};
        int max = a[0];
        for (int i = 0; i < a.length; i++) {
            if (max < a[i]) {
                max = a[i];
            }
        }
        System.out.println(max);
    }
}


   9,计算两个整型数组的和:
     {1,2,3,4}+{3,4,5,6}={4,6,8,10}
     {1,2,3,4,5}+{3,4,5,6}={4,6,8,10,5}
    {1,2,3,4}+{3,4,5,6,7}={4,6,8,10,7}

package com.Arry;

/**
 * 9,计算两个整型数组的和:
 *      {1,2,3,4}+{3,4,5,6}={4,6,8,10}
 *      {1,2,3,4,5}+{3,4,5,6}={4,6,8,10,5}
 *     {1,2,3,4}+{3,4,5,6,7}={4,6,8,10,7}
 */
public class Demo10 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4};
        int[] b = {3, 4, 5, 6};
        int[] arr = new int[a.length];
        for (int i = 0; i < a.length; i++) {
            arr[i] = a[i] + b[i];
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}


   10,找出数组中重复出现的元素,如{1,2,3,4,3,4,6,7},找出3,4两个

package com.Arry;

/**
 * 10,找出数组中重复出现的元素,如{1,2,3,4,3,4,6,7},找出3,4两个
 */
public class Demo11 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 3, 4, 6, 7};
        for (int i = 0; i < arr.length; i++) {
            for (int j= i+1 ;j<arr.length;j++){
                if (arr[i] == arr[j]){
                    System.out.println(arr[i]);
                }
            }
        }
    }
}


   11,从键盘依次输入10个正整数,要求每次存储到数组中时,保证其顺序为升序(从小到大)
      {3,5,8,9,10,10,0,0,0,0,0}

package com.Arry;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 11,从键盘依次输入10个正整数,要求每次存储到数组中时,保证其顺序为升序(从小到大)
 *       {3,5,8,9,10,10,0,0,0,0,0}
 */
public class Demo12 {
    public static void main(String[] args) {
        int[] nums = new int[10];
        Scanner sc = new Scanner(System.in);
        int c = 1;//控制计算下标位置的循环次数
        for (int i = 0; i < nums.length; i++) {
            System.out.println("请输入第"+(i+1)+"个元素:");
            int a = sc.nextInt();
            int count = 0;//count为应该插入的位置
            for (int j = 0; j < c; j++) {
                if (a > nums[j]) {
                    count++;
                }
            }
            count=count-1;//第一个应该在位置0
            c++;
            int b = 0;
            //整体向后移直到count
            for (int j = nums.length - 2; j >= count; j--) {
                nums[j + 1] = nums[j];
            }
            //插入
            nums[count] = a;
            System.out.println(Arrays.toString(nums));//为了更直观将每一步的排序结果显示
        }
        System.out.println(Arrays.toString(nums));
    }
}


   12,产生双色球的数列,规则如下:前6位是1-33之间不重复的随机数,第7位是1-16之间的一个随机数

package com.Arry;

import java.util.Arrays;

/**
 *   12,产生双色球的数列,规则如下:前6位是1-33之间不重复的随机数,第7位是1-16之间的一个随机数
 */
public class Demo13 {
    public static void main(String[] args) {
        int[] nums = new int[7];
        c: for (int i = 0;i<nums.length; i++) {
            int b = (int) (Math.random() * 33) + 1;
            // 如果产生随机数等于之前存入数组的数跳过此次循环
            for (int j = 0; j < nums.length - 1; j++) {
                if (b == nums[j]) {
                    continue c;
                }
            }
            nums[i] = b;
        }
        nums[6] = (int) (Math.random() * 16) + 1;
        System.out.println(Arrays.toString(nums));
    }
}



   13,判断一个数组是否包含另一个数组,如{1,2,3,4,5} 包含 {2,3,4},不包含{3,5}

package com.Arry;

import java.util.Arrays;

/**
 *    13,判断一个数组是否包含另一个数组,如{1,2,3,4,5} 包含 {2,3,4},不包含{3,5}
 */
public class Demo14 {
    public static void main(String[] args) {
        int[] num1s = {1, 2, 3, 4, 5, 8, 9};
        int[] num2s = {4, 5, 9};
        int[] num3s = new int[num2s.length];
        // 循环比较数组二和数组三
        for (int i = 0; i < num1s.length - num2s.length + 1; ) {
            // 将数组二长度的值从数组一中赋给数组三
            for (int j = 0; j < num2s.length; j++, i++) {
                num3s[j] = num1s[i];
            }
            i--;
            // 比较数组二和数组三是否相等
            if (Arrays.equals(num2s, num3s)) {
                System.out.println("包含。");
                break;
            } else if (i == num1s.length - num2s.length) {
                System.out.println("不包含。");
            }
        }
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值