一枚渣子


私信TA

用户名:uq_44501180482

访问量:14724

签 名:

笨蛋

等  级
排  名 24
经  验 15975
参赛次数 1
文章发表 238
年  龄 0
在职情况 在职
学  校 河南科技大学
专  业

  自我简介:

渣子

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		ArrayList<Integer>arr=new ArrayList<>();
		for (int i = 0; i < n; i++) {
			arr.add(0,sc.nextInt());
		}
		int m=sc.nextInt();
		for (int i = 0; i < m+1; i++) {
			String str=sc.nextLine();
              if(!str.equals("")){
			  String[]s=str.split(" ");
			  
				if(s[0].equals("show")){
					int k=arr.size();
					if(k==0)System.out.println("Link list is empty");
					else{
						for (int j = 0; j < k-1; j++) {
							System.out.print(arr.get(j)+" ");
						}
						System.out.println(arr.get(k-1));
					}
				}else if(s[0].equals("insert")){
					int a=Integer.parseInt(s[1]);
					int b=Integer.parseInt(s[2]);
					try {
						arr.add(a-1,b);
						System.out.println("insert OK");
					} catch (Exception e) {
						System.out.println("insert fail");
					}
				}
				else if(s[0].equals("delete")){
					int a=Integer.parseInt(s[1]);
					try {
						arr.remove(a-1);
					    System.out.println("delete OK");
					} catch (Exception e) {
						System.out.println("delete fail");
					}
				}
				else {
					int a=Integer.parseInt(s[1]);
					try {
					    System.out.println(arr.get(a-1));
					} catch (Exception e) {
						System.out.println("get fail");
					}
				}
			}
		}
	}
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区