数组相关练习

1.  输出一个菜单:

    1.查看数组 

    2.修改数据

    3.删除数据

    并在选中后,继续实现该代码。

    注意:2 修改数据中, 输入数据,和编号 进行修改。且编号不对继续输入,直到修改成功。


代码书写:

import java.util.Scanner;

public class ShuZu {

public static void main(String[] args) {

int[] a = { 5, 9, 2, 8, 7 }; 
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println("a");
System.out.println("1.查看数据");
System.out.println("2.修改数据");
System.out.println("3.删除");
Scanner S = new Scanner(System.in);
System.out.println("选择为:");
int B = S.nextInt();
switch (B) {
case 1:
Scanner M = new Scanner(System.in);
System.out.println("输入下标");
int d = M.nextInt();
System.out.println("a[" + d + "]=" + a[d]);
break;
case 2:
Scanner X = new Scanner(System.in);
System.out.println("输入数据:");
int m = X.nextInt();
Scanner Y = new Scanner(System.in);
System.out.println("输入下标");
int j = Y.nextInt();
int k;
if (j < a.length) {
a[j] = m;
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
} else {
System.out.println("超出数组长度");
System.out.println();
do {
Scanner Z = new Scanner(System.in);
System.out.println("超出数组长度");
System.out.println();
System.out.println("输入下标");
k = Z.nextInt();
} while (k > a.length);
a[k] = m;
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
break;
case 3:
Scanner H = new Scanner(System.in);
System.out.println("输入删除坐标");
int h = H.nextInt();
a[h] = 0;
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
break;
}
}

                 }

运行结果为:


其中第二部分:

可以写为:

case 2:

int onedata = input.nextInt( ) ;

int index =-1 ;           //这里等于-1是为了让他首先执行一次

int [ ] a ={ 5,3,6,9,1} ;

while( true){

   System.out.println("输入一个数组下标:") ;

   index=input.nextInt( ) ;

   if(index >= a.length){               // 判断是否超出数组范围

     System.out.println( "提示:超出数组范围") ;

  }else {                                      //否则 退出循环 执行修改

    break ;

}

a[index]=onedata ;

for(int i=0  ;i< a.length ; i++){

  System.out.println( a[i]+",");



2. 定义一个数组,然后给出一个数字,利用该数字在该数组中查找。输出该数字在在数组中的坐标,如果找不到,则输出找不到。

 

代码书写为:

import java.util.Scanner;


public class ShuZu {


public static void main(String[] args) {

int[] diu = { 1, 3, 7, 1, 2, 4 };
Scanner x = new Scanner(System.in);
System.out.println("输入数据:");
int s = x.nextInt();
int index = 0;
for (int i = 0; i < diu.length; i++) {
if (diu[i] == s) {
System.out.println("下标为:" + i);
} else {
index++;        //   每找一次 找不到 index就加1,index初值为0,如果第六次没有找到,那么index就为6。
}
}
if (index == diu.length) {
System.out.println("找不到");
}
}
}


运行结果:


这里合理的运用了定义一个变量,用变量的计数来解决问题。


3. JAVA矩阵数组的对角相加。

代码书写为:

import java.util.Scanner;

public class ShuZu {

public static void main(String[] args) {

 int[][] a = { { 1, 2, 3 }, { 2, 3, 4 },{3,8,1} };
int b = 0;
int c = Math.min(a.length, a[0].length);      //   矩阵取对角的值,应该取行和列中较小的方形对角线,取大的会超范围。
for (int i = 0; i < c; i++) {
b += a[i][i];
}    
System.out.println("合为:" + b);
System.out.println("行:" + a.length);
System.out.println("列:" + a[0].length);
}
}

运行结果为:



4. 将一个String类型的字符串,转变为char 型数组。

    字符串随意定义。

代码书写为:

public class ShuZu {

public static void main(String[] args) {

                 String sb = "rfghjklvgbhjk";
char[] sx = sb.toCharArray();
for (int i = 0; i < sx.length; i++) {
System.out.println("sx[" + i + "]=" + sx[i]);
}
}
}

运行结果为:



5.定义两个数组a,b 。

    将a,b合并为数组c

代码书写为:

public class ShuZu {

public static void main(String[] args) {

               int[] a = { 3, 1, 5, 7, 3 };
int[] b = { 4, 6, 1, 6, 2 };
int[] c = new int[a.length + b.length];
for (int i = 0; i < a.length; i++) {
c[i] = a[i];
}
for (int j = a.length; j < c.length; j++) {
c[j] = b[j - a.length];
}
for (int m = 0; m < c.length; m++) {
System.out.println("c[" + m + "]=" + c[m]);
}
}
}


运行结果为:



6.定义一个10个元素的数组,里面的数字为1-10的倒序排列,然后遍历。

   然后取出数组的所有元素,并输出他们的和。

  public class ShuZu {

public static void main(String[] args) {

                 int[] diu = new int[10];
int m = 0;
for (int i = 10; i > 0;i-- ) {
diu[10 - i] = i;
System.out.print(diu[10-i] + " ");
}
System.out.println();
for (int j = 0; j < diu.length; j++) {
m += diu[j];
}
System.out.println("数组和: " + m);
}
}

运行结果为:


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值