码迷,mamicode.com
首页 > 编程语言 > 详细

数组的简单练习题

时间:2019-04-23 12:38:05      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:++   习题   nbsp   pac   imp   练习   public   code   count   

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

package Array;

public class arrayTest1 {
    public static void main(String[] args) {
        int[] a={1,2,3,4,5,6};
        int m=a.length;
        int temp;
        for (int i = 0; i <m/2; i++) {
           temp=a[i];
           a[i]=a[m-i-1];
           a[m-i-1]=temp;
        }
        for (int i = 0; i < m; i++) {
            System.out.print(a[i]+" ");
        }
    }
}

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} ;

package Array;

public class arrayTest2 {
    public static void main(String[] args) {
        int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
        int count=0;
        for (int i = 0; i <oldArr.length ; i++) {
            if(oldArr[i]!=0){
                count++;
            }
        }

        int[] newArr=new int[count];
        for (int i = 0,j=0; i <oldArr.length ; i++) {
            if(oldArr[i]!=0)
            {
                newArr[j]=oldArr[i];
                j++;
            }
        }
        System.out.print("新数组为:");
        for (int i = 0; i < count; i++) {
            System.out.print(newArr[i]+" ");
        }
    }
}

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

package Array;

import java.util.ArrayList;
import java.util.List;

public class arrayTest3 {
    public static void main(String[] args) {
        int[] a = {1, 7, 9, 11, 13, 15, 17, 19};
        int[] b = {2, 4, 6, 8, 10};
        int m = a.length + b.length;
        int j = 0,temp;
        int[] c = new int[m];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i];
        }
        for (int i = a.length; i < m; i++) {
            c[i] = b[j];
            j++;
        }

        for(int i=0;i<m-1;i++){
        for(int s=0;s<m-1-i;s++){
         if(c[s]>c[s+1]){
            temp=c[s];
            c[s]=c[s+1];
            c[s+1]=temp;
            }
        }
    }
        for (int i = 0; i < m; i++) {
            System.out.print(c[i] + " ");
        }
    }
}

 

数组的简单练习题

标签:++   习题   nbsp   pac   imp   练习   public   code   count   

原文地址:https://www.cnblogs.com/almm/p/10755426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!