Java 保存ArrayList条目
首先,这是我的代码 posit.java文件Java 保存ArrayList条目,java,arraylist,Java,Arraylist,首先,这是我的代码 posit.java文件 package postit; import java.util.Scanner; class Postit { public static Scanner menu = new Scanner(System.in); public static void main(String[] args) { int MenuOption = 0; NewStorage G = new NewStorage(); // Ca
package postit;
import java.util.Scanner;
class Postit {
public static Scanner menu = new Scanner(System.in);
public static void main(String[] args) {
int MenuOption = 0;
NewStorage G = new NewStorage(); // Case 1 Object
while(MenuOption != 3){
System.out.println(
"\n--------Note System-------\n" +
"----------------------------\n" +
"1. Create a Note \n" +
"2. View Notes \n" +
"3. Close Program\n" +
"----------------------------\n");
MenuOption = menu.nextInt();
menu.nextLine();
switch (MenuOption) {
case 1:
G.printinfo();
G.Notestore();
break;
case 2:
G.viewNotes();
G.printNotes();
break;
case 3:
System.out.println("Program is closing");
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
package postit;
import java.util.Scanner;
import java.util.ArrayList;
class NewStorage {
ArrayList<Note> NoteArray = new ArrayList<Note>(20);
public void printinfo() {
System.out.println("--- Fill note here ---");
}
public void Notestore() {
System.out.println("Enter the note ID you wish to attach the note with\n\n");
String inputIDnote = Postit.menu.nextLine();
System.out.println("Enter your note\n\n");
String noteDescription = Postit.menu.nextLine();
NoteArray.add(new Note(inputIDnote, noteDescription));
}
public void viewNotes() {
System.out.println("Please enter the number of the note you wish to view.");
int count = 0;
for (int i = 0 ; i < NoteArray.size(); i++) {
System.out.println((count++) + ": " + NoteArray.get(i).inputIDnote);
}
}
public void printNotes(){
int count = Postit.menu.nextInt();
Postit.menu.nextLine();
System.out.println(count + " " + NoteArray.get(count));
}
}
package postit;
class Note {
String inputIDnote;
String noteDescription;
public Note(String inputIDnote, String noteDescription) {
this.inputIDnote = inputIDnote;
this.noteDescription = noteDescription;
}
@Override
public String toString() {
return "ID: " + inputIDnote + " \n\nDescription: " + noteDescription;
}
}
NewStorage.java文件
package postit;
import java.util.Scanner;
class Postit {
public static Scanner menu = new Scanner(System.in);
public static void main(String[] args) {
int MenuOption = 0;
NewStorage G = new NewStorage(); // Case 1 Object
while(MenuOption != 3){
System.out.println(
"\n--------Note System-------\n" +
"----------------------------\n" +
"1. Create a Note \n" +
"2. View Notes \n" +
"3. Close Program\n" +
"----------------------------\n");
MenuOption = menu.nextInt();
menu.nextLine();
switch (MenuOption) {
case 1:
G.printinfo();
G.Notestore();
break;
case 2:
G.viewNotes();
G.printNotes();
break;
case 3:
System.out.println("Program is closing");
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
package postit;
import java.util.Scanner;
import java.util.ArrayList;
class NewStorage {
ArrayList<Note> NoteArray = new ArrayList<Note>(20);
public void printinfo() {
System.out.println("--- Fill note here ---");
}
public void Notestore() {
System.out.println("Enter the note ID you wish to attach the note with\n\n");
String inputIDnote = Postit.menu.nextLine();
System.out.println("Enter your note\n\n");
String noteDescription = Postit.menu.nextLine();
NoteArray.add(new Note(inputIDnote, noteDescription));
}
public void viewNotes() {
System.out.println("Please enter the number of the note you wish to view.");
int count = 0;
for (int i = 0 ; i < NoteArray.size(); i++) {
System.out.println((count++) + ": " + NoteArray.get(i).inputIDnote);
}
}
public void printNotes(){
int count = Postit.menu.nextInt();
Postit.menu.nextLine();
System.out.println(count + " " + NoteArray.get(count));
}
}
package postit;
class Note {
String inputIDnote;
String noteDescription;
public Note(String inputIDnote, String noteDescription) {
this.inputIDnote = inputIDnote;
this.noteDescription = noteDescription;
}
@Override
public String toString() {
return "ID: " + inputIDnote + " \n\nDescription: " + noteDescription;
}
}
所以请原谅我,因为我正在学习如何编写Java,所以这是我的第一个程序,所以如果你认为它写得很糟糕,那么你知道为什么
正如您所看到的,程序可以询问用户是否希望创建一个带有ID和便笺本身的便笺。然后,用户可以通过选择所列注释ID旁边的编号来查看创建的注释。所以当我关闭程序时,ArrayList显然被删除了。是否有办法在程序关闭后保存数组列表中的注释?因为我想稍后实现一个编辑功能,所以它将是一个有用的东西。现在您需要了解
文件
,文件编写器
/文件阅读器
或缓冲阅读器
/缓冲编写器
。他总是有很好的辅导课。我认为你应该创建数据库来保存数组,从那里你可以访问数组进行编辑或从数组中删除。下一步是处理文件。请阅读有关持久性的内容。可能基于文件是最简单的,或者说是关系数据库。