zoukankan      html  css  js  c++  java
  • Java数组合并

    作业目的:将两个数组合并为一个数组,并排序

    源码如下:

    public class jh_30_数组合并 {
    /**
    * 1.申请一个新的数组,新的数组的长度是两个数组的长度之和
    * 2.分别把两个数组中对应的元素给赋值到新的数组中
    *
    * @param args
    */
    public static void main(String[] args) {
    int[] arr1 = {3, 1, 23};
    int[] arr2 = {27, 7, 2};
    // 新的数组长度是两个数组的长度之和
    int[] newArr = new int[arr1.length + arr2.length];
    // 定义一个变量记录对应的长度
    int count = 0;
    // 给newArr数组赋值为arr1数组的值,并记录长度
    for (int i = 0; i < arr1.length; i++) {
    newArr[i] = arr1[i];
    count++;
    }
    for (int j = 0; j < arr2.length; j++) {
    // newArr[count] = arr2[j];
    // count++;
    // 可以简写为
    newArr[count++] = arr2[j];
    // count++:先参与运算,count的值再自增1
    // ++count:count的值先自增1,再参与运算
    }
    for (int i = 0; i < newArr.length; i++) {
    System.out.print(newArr[i] + " ");
    }
    }
    }
  • 相关阅读:
    chapter4.6生成器
    chapter4.4、递归
    chapter4.3、函数执行流程
    chapter4.2、函数返回值
    直接插入排序
    打印三角型的练习
    杂记
    linux top命令
    makefile 中的 := , += ,?=
    makefile中的shell语法 || Makefile中的@
  • 原文地址:https://www.cnblogs.com/LEPENGYANG/p/14980690.html
Copyright © 2011-2022 走看看