• Java数组练习


    需求

    1.将一个给定的整型数组转置输出,
    例如: 源数组,1 2 3 4 5 6
    转置之后的数组,6 5 4 3 2 1

    2.现在有如下的一个数组:
    int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
    要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
    int[] newArr = {1,3,4,5,6,6,5,4,7,6,7,5} ;

    3.现在给出两个数组:
    数组a:"1,7,9,11,13,15,17,19"
    数组b:"2,4,6,8,10"
    两个数组合并为数组c。

    思路

    1. (1)给任意一个确定的数组,作为参数;

      (2)定义一个函数,完成转置数组的功能(用冒泡排序从大到小完成排序),然后返回该数组;

      (3)再主方法里面调用该函数,并传入一个确定的数组,返回转置后的数组,然后遍历查看;

    2. (1)定义一个函数,参数为一个整形数组,先判断里面非零的个数;然后再创建一个新数组,

      再把旧数组里面的非零值赋值给新的数组;最后返回新数组;

      (2)主方法里面定义一个数组,以该数组为参数,调用函数,返回一个新的数组,然后遍历

      新数组查看

    3. (1)定义一个函数,完成合并两个数组,并返回合并之后的数组

      (2)定义两个数组,调用函数,返回一个数组并遍历查看它

    源码

    1.

     1 package one;
     2 
     3 /**
     4  * @author xjs
     5  * @date 2019/4/22 - 23:11
     6  */
     7 
     8 public class Test1 {
     9 
    10     public static int[] fun(int [] arr){
    11         //冒泡排序--把最小的放最后面
    12         for (int i=0;i<arr.length-1;i++){//length-1--要排到最后的数的个数为n-1
    13             for(int j=0;j<arr.length-1-i;j++){//length-i趟------两两比较的次数 n-1-i,而且每次比较下标都是从0开始
    14                 if(arr[j]<arr[j+1]){
    15                     int temp=arr[j];
    16                     arr[j]=arr[j+1];
    17                     arr[j+1]=temp;
    18                 }
    19             }
    20         }
    21         return arr;
    22     }
    23 
    24     public static void main(String[] args) {
    25         int[] arr={1,2,3,4,5,6};
    26         arr=Test1.fun(arr);
    27 
    28         for (int i = 0; i < arr.length; i++) {
    29             System.out.print(arr[i]+" ");
    30         }
    31     }
    32 }

    2.

     1 package one;
     2 
     3 /**
     4  * @author xjs
     5  * @date 2019/4/23 - 8:50
     6  */
     7 
     8 public class Test4 {
     9     /*函数:通过传来一个数组,先判断里面的非零个数,然后创建
    10     一个新数组,再把旧数组里面的非零数赋值到新的数组里面,
    11     最后返回新的数组*/
    12     public static int[] fun(int oldArr[]){
    13         int k=0;
    14         for (int i = 0; i < oldArr.length; i++) {
    15             if (oldArr[i]==0){
    16                 k++;
    17             }
    18         }
    19         int m=oldArr.length-k;
    20         int[] newArr=new int[m];
    21         int j=0;
    22         for (int i = 0; i < oldArr.length; i++) {
    23             if (oldArr[i]!=0){
    24                 newArr[j]=oldArr[i];
    25                 j++;
    26             }
    27         }
    28         return newArr;
    29     }
    30     
    31     public static void main(String[] args) {
    32         int[] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
    33         int[] newArr=fun(oldArr);
    34         for (int i = 0; i < newArr.length; i++) {
    35             System.out.print(newArr[i]+" ");
    36         }
    37     }
    38 }

    3.

     1 package one;
     2 
     3 /**
     4  * @author xjs
     5  * @date 2019/4/23 - 8:23
     6  */
     7 
     8 public class Test3 {
     9 
    10     public static int[] fun(int[] a,int[] b){
    11         int m=a.length+b.length;
    12         int[] c=new int[m];
    13         for (int i = 0; i <c.length ; i++) {
    14             if (i<a.length){
    15                 c[i]=a[i];
    16             }else {
    17                 c[i]=b[i-a.length];
    18             }
    19         }
    20         return c;
    21     }
    22 
    23     public static void main(String[] args) {
    24         //可将任意确定的数组传进函数,然后合并
    25         int[] a={1,7,9,11,13,15,17,19};
    26         int[] b={2,4,6,8,10};
    27         int[] c=fun(a,b);
    28         for (int i = 0; i < c.length; i++) {
    29             int i1 = c[i];
    30             System.out.print(i1+" ");
    31         }
    32     }
    33 }
  • 相关阅读:
    leetcode131分割回文串
    leetcode315 计算右侧小于当前元素的个数
    Tensorflow写代码流程&反向传播
    vue脚手架的搭建
    Vue 脱坑记
    简历中的工作经历要怎么写?
    如何制作高水平简历?
    window.location.hash的知识点
    前端面试题小集
    前端面试题
  • 原文地址:https://www.cnblogs.com/xjs1874704478/p/10754868.html
Copyright © 2020-2023  润新知