基础Java数组练习题及答案转自http://wenku.baidu.com/view/3ab2f732580216fc700afd39.html

在开发的时候主方法之中的代码越少越好。

 

1

 

将一个给定的整型数组转置输出,

 

 

例如:

 

源数组,

1 2 3 4 5 6 

 

 

 

转置之后的数组,

6 5 4 3 2 1 

public class MyDemo { 

 

public static void main(String args[]){ 

 

 

int [] data = new int[7]  

 

 

init(data)  

// 

将数组之中赋值

 

 

 

print(data)  

 

 

System.out.println()  

 

 

reverse(data)  

 

 

print(data)  

 

 

public static void reverse(int temp[]){ 

 

 

int center = temp.length / 2  // 

求出中心点

 

 

 

int head = 0  

// 

表示从前开始计算脚标

 

 

 

int tail = temp.length - 1  

// 

表示从后开始计算脚标

 

 

 

for(int x = 0  x<center  x++){ 

 

 

 

int t = temp[head]  

 

 

 

 

temp[head] = temp[tail]  

 

 

 

temp[tail] = t  

 

 

 

head ++  

 

 

 

tail --  

 

 

 

 

public static void init(int temp[]){ 

 

 

for(int x = 0  x < temp.length; x++){ 

 

 

 

temp[x] = x + 1  

 

 

 

 

public static void print(int temp[]){ 

 

 

for(int x = 0  x< temp.length  x++){ 

 

 

 

System.out.print(temp[x] + "

")  

 

 

 

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}  

思路:

生活中的问题解决

 

程序中的解决;

 

 

1

 

确定出不为

0

的个数,这样可以开辟新数组;

 

 

2

 

从旧的数组之中,取出内容,并将其赋给新开辟的数组;

 

public class MyDemo { 

 

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 newArr [] = new int[count(oldArr)]  // 

新数组

 

 

 

fun(oldArr,newArr)  

 

 

print(newArr)  

 

 

public static void fun(int src[],int data[]){ 

 

 

int foot = 0  

// 

控制新数组的脚标,

data 

 

 

for(int x = 0  x < src.length; x++){ 

 

 

 

if(src[x] != 0){ 

 

 

 

 

data[foot++] = src[x]  

 

 

 

 

 

 

 

public static int count(int temp[]){ 

 

 

int num = 0  

 

 

for(int x = 0  x < temp.length; x++){ 

 

 

 

if(temp[x] != 0){ 

 

 

 

 

num ++  

// 

统计个数

 

 

 

 

 

 

 

 

return num  

 

 

public static void print(int temp[]){ 

 

 

for(int x = 0  x< temp.length  x++){ 

 

 

 

System.out.print(temp[x] + "

")  

 

 

 

3

 

现在给出两个数组:

 

 

·

 

数组

A

1

7

9

11

13

15

17

19

 

 

·

 

数组

b

2

4

6

8

10

 

 

 

 

 

 

 

两个数组合并为数组

c

,按升序排列。

 

public class MyDemo { 

 

public static void main(String args[]){ 

 

 

int data1 [] = new int[] {1,7,9,11,13,17,19}  

 

 

int data2 [] = new int[] {2,4,6,8,10}  

 

 

int newArr [] = concat(data1,data2)  

 

 

java.util.Arrays.sort(newArr)  

 

 

print(newArr)  

 

 

public static int[] concat(int src1[],int src2[]){ 

 

 

int len = src1.length + src2.length  

// 

新数组的大小

 

 

 

int arr[] = new int[len]  

 

 

 

// 

新数组

 







 

 

System.arraycopy(src1,0,arr,0,src1.length)  

// 

拷贝第一个数组

 

 

 

System.arraycopy(src2,0,arr,src1.length,src2.length)  

// 

拷贝第二个数组

 

 

 

return arr  

 

 

public static void print(int temp[]){ 

 

 

for(int x = 0  x< temp.length  x++){ 

 

 

 

System.out.print(temp[x] + "

")  

 

 

 

 

主要的目的是熟悉这两个操作的方法,数组扩大,必须要将原始数组的内容拷贝进去。

http://wenku.baidu.com/view/3ab2f732580216fc700afd39.html


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值