我已经写了我能写的,并且已经到了我无法弄清楚为什么它不保存数据的地步。下面是文件测试,然后是文件和我制作的数组文件。请帮忙...
$
import java.util.Scanner;
public class FriendsListTest
{
public static void main( String[] args)
{
FriendsList friendsList = new FriendsList();
char selection;
Scanner input = new Scanner( System.in );
do
{
//display menu
System.out.println("\n--------------");
System.out.println("Main Menu\n");
System.out.println("1. Add a friend ");
System.out.println("2. Delete a friend ");
System.out.println("3. Show List ");
System.out.println("4. Exit");
//selection results
selection = input.next().charAt(0);
switch(selection){
case '1':
FriendsList.AddFriends();
break;
case '2':
FriendsList.RemoveFriend();
break;
case '3':
FriendsList.DisplayArray();
break;
case '4':
break;
default:
System.out.println("Invalid Selection");
}//end Switch
}while( selection != 4);//end do
}//end main
}//end FriendsListTest
$ $
import java.util.Scanner;
import java.util.ArrayList;
public class FriendsList
{
public static void AddFriends()
{
Scanner input = new Scanner( System.in );
ArrayList<Friends> friend = new ArrayList<Friends>(20);
System.out.println("\n--------------");
System.out.println("Please enter Name");
String name = input.nextLine(); //reads name
System.out.println("Please enter Age");
int Age = input.nextInt();
friend.add(new Friends( Age, name));
}//end AddFriends
public static void RemoveFriend()
{
ArrayList<Friends> friend = new ArrayList<Friends>(20);
Scanner input = new Scanner( System.in );
System.out.println("\n--------------");
System.out.println("Please enter Name of person you wish you remove");
String name = input.nextLine();
friend.remove(name);
}//End RemoveFriend
public static void DisplayArray()
{
ArrayList<Friends> friend = new ArrayList<Friends>(20);
System.out.print(friend);
}//end DisplayArray
}//end class FriendsList
$ $
class Friends
{
private String nName;
private int nAge;
public Friends(int Age, String name)
{
nName = name;
nAge = Age;
}
public String getName()
{
return nName;
}
public int getAge()
{
return nAge;
}
}//end class person
好的,根据您的建议,我能够让它存储数据,但现在我遇到了删除数据的问题。删除数据时它不会抛出任何错误,但数据仍然存在。如果您使用我所做的编辑,我会重新发布该程序。