1 / 3
文档名称:

基础Java数组练习题及答案.docx

格式:docx   大小:18KB   页数:3页
下载后只包含 1 个 DOCX 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

基础Java数组练习题及答案.docx

上传人:小熙 2021/12/9 文件大小:18 KB

下载得到文件列表

基础Java数组练习题及答案.docx

文档介绍

文档介绍:基础Java数组练****题及答案
基础Java数组练****题及答案
基础Java数组练****题及答案
在开发的时候主方法之中的代码越少越好。
1、
将一个给定的整型数组转置输出,
例如:
源数组, 123456
转置之后的数组, 6 5 4 3 2 1
public class MyDemo {
public static void main(String args[]){
int [] data = new int[7] ;
init(data) ; // 将数组之中赋值
print(data) ;
;
reverse(data) ;
print(data) ;
}
public static void reverse(int temp[]){
int center = / 2 ; // 求出中心点
int head = 0 ; // 表示从前开始计算脚标
int tail = - 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 < ; x++){
temp[x] = x + 1 ;
}
}
public static void print(int temp[]){
for(int x = 0 ; x< ; 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