Java思路:使用String的字符串处理以及ArrayList的列表处理。
Java代码:
import java.util.ArrayList; import java.util.Scanner; public class Main{ public static ArrayList<String> list = new ArrayList<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.nextLine(); for (int i = 0; i < n; i++) { String str = sc.nextLine(); String[] strArr = str.split(" "); if ("ls".equals(strArr[0])){ for (int j = 0; j < list.size(); j++) { System.out.println(list.get(j)); } }else if ("rm".equals(strArr[0]) && list.indexOf(strArr[1]) != -1){ list.remove(list.indexOf(strArr[1])); }else if ("touch".equals(strArr[0]) && list.indexOf(strArr[1]) == -1){ list.add(strArr[1]); }else rename(strArr[1],strArr[2]); } } public static void rename(String fromName,String toName){ if (list.indexOf(fromName) == -1){ list.add(toName); }else { list.set(list.indexOf(fromName),toName); } } }