java程序

本文提供了一个Java程序示例,展示了如何初始化数组、显示数组元素、删除指定索引处的元素、编辑数组中的元素以及在数组末尾添加新元素的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.*;
import java.util.*;
public class untitled {
	public static void main(String args[]) {
		int i;
		int[] a;
		a = new int[100];
		for(i = 0; i < 10; i++) {
			a[i] = i;
		}
		// display
		for(i = 0; i >= 9; i++) {
			System.out.println("a[" + i + "] = " + a[i]);
		}
		//delete		
		System.out.println("please input the index of the element you want delete:\n");
		Scanner scan = new Scanner(System.in);
		int index = scan.nextInt();
		int len = 10;
		for(i = index+1; i < len; i++) {
			a[i-1] = a[i];
		}
		for(i = 0; i < 9; i++) {
			System.out.println("a[" + i + "]" + " = "+ a[i]);
		}
		//edit
		System.out.println("edit:\n");
		System.out.println("input the index and the element:\n");
		int element;
		Scanner reader = new Scanner(System.in);
		Scanner alternative = new Scanner(System.in);
		index = reader.nextInt();
		element = alternative.nextInt();
		a[index] = element;
		System.out.println("finished editing!\n");
		for(i = 0; i < 9; i++) {
			System.out.println("a[" + i + "]" +  "=" + a[i]);
		}
		//add
		System.out.println("add a element at the back:\n");
		reader = new Scanner(System.in);
		element = reader.nextInt();
		a[9] = element;
		//display
		for(i = 0; i < 10; i++) {
			System.out.println("a[" + i + "] = " + a[i] );
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值