簡體   English   中英

數組? 或不?

[英]Arrays? or not?

我不是Java方面的專家,但是我需要為我的課程主題解決此問題/活動,所以我才真正需要您的幫助。 我有編程問題。 問題是我不知道應該使用什么方法或Java代碼來解決此問題:

創建一個班級通訊錄,其中可以包含100個姓名,地址,聯系電話和電子郵件地址條目。 您應該為通訊簿提供以下方法:

添加條目,刪除條目,查看所有條目和更新條目

更新:這是我到目前為止得到的代碼

我想我可以為此使用2d數組 ,但是,一旦我開始編碼, 我就無法真正繼續下去 ,我不知道是否可以在這種活動中使用數組。 我嘗試搜索其他Java代碼,但是我越了解可能的新技術或代碼,就越會混淆我必須使用的代碼!

如果有人可以幫助我為該活動構建代碼,我將非常感謝並一定會研究地獄將如何/您能做到這一點! 因為我對學習Java真的很感興趣,所以我只需要一些幫助就可以實現該目的。 提前致謝!

這就是我想知道的代碼:這些程序的功能僅是用於添加編輯視圖和刪除名稱,iam仍在弄清楚如何在數組中添加維,還是應該添加? 還是如果不是數組? 怎么樣? 在該活動達到要求之前,我該如何回答:(

package javaactivities;
import java.util.*;
import java.util.Scanner;

public class AddressBook {
        static  List<String> l=new ArrayList<String>();

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        boolean y=true;  
 do{   
     System.out.println("Enter \n1 to add an entry\n2 to edit entry");
     System.out.println("3 to delete an entry\n4 to view entries\n5 to exit");
     System.out.print("enter your choice: ");
     int choice=in.nextInt();
     switch(choice)
     {
        case 1:
            insert();
            break;
        case 2:
            edit();
            break;
        case 3:
            delete();
            break;
        case 4:
            print();
            break;
        case 5:
            toexit();
            break;
        default:
            System.out.println("bad input");
            break;
    }
    System.out.println("want to process more? y/n");
    String x=in.next();
    char ch=x.charAt(0); 
    if( ch=='n')
        y=false;
}
while(y!=false);

}

static public void insert(){


   Scanner in=new Scanner(System.in);

    boolean y=true;
   do{
        System.out.println("enter name to add in list");
        String entry=in.next();
            l.add(entry);
        System.out.println("want to insert more?y/n");
        String x=in.next();
        char ch=x.charAt(0);
        if( ch=='n')
            y=false;
     }
   while(y!=false); 

}

static public void print(){


   if(l.isEmpty())
       System.out.println("list is empty ");
   else
        System.out.println("members of lists are:");
        for(int i=0 ; i<l.size();i++)
            System.out.println("Entry "+i+" : "+ l.get(i)+" ");

}

static public void edit(){


   Scanner in=new Scanner(System.in);

   String num2;
   System.out.println("enter name you want to add");
   num2=in.next();
         try{
            System.out.println("enter entry # of the name you want to edit");
            int num1=in.nextInt();
            l.set(num1, num2);
         }catch(IndexOutOfBoundsException e){
                System.err.println("caught IndexOutOfBoundsException: specified position is empty "+e.getMessage());
           }

      }



static public void delete(){


    Scanner in=new Scanner(System.in);
    System.out.println("enter entry # you want to delete");
    int num=in.nextInt();
    l.remove(num);

}

static public void toexit(){

    System.exit(0);
}

}

由於您有幾個條目要存儲,因此我的建議是一個鏈表。 鏈表是一種數據結構,您可以在其中存儲多個條目。Java提供了一個

http://www.mycstutorials.com/articles/data_structures/linkedlists此鏈接將幫助您獲取鏈接列表。

首先,實現所有必需的類,在您的情況下,兩個:

  • 地址簿
  • 條目

骨架可能是這樣的

public final class AddressBook {
  public static final class Entry {
    private String name; 
    private String address; 
    private String contactNumber;      
    private String email;  

    private Entry(String name, String address, String contactNumber, String email) {
      this.name = name;
      this.address = address;
      this.contactNumber = contactNumber;
      this.email = email;
    }

    public String getName() {return name;}
    public String getAddress() {return address;}
    public String getContactNumer() {return contactNumber;}
    public String getEmail() {return email;}
  }

  private ArrayList<Entry> entries = new ArrayList<Entry>();

  public AddressBook() {;}

  public int size() {return entries.size();}
  public int get(int index) {return entries.get(index);}
  ...
  public Entry add(String name, String address, String contactNumber, String email) {
    Entry entry = new Entry(name, address, contactNumber, email);

    entries.add(entry);

    return entry;
  }
  ...
}

為了實現viewAll()您可以選擇在兩個類中都覆蓋toString()方法,對於delete()似乎也必須實現find()等。然后只需使用這些類

  public final class Main {
    private static AddressBook book = new AddressBook();

    public static void main(String[] args) {
      ...
      switch(choice) {
        case 1: 
          book.add(...);
          break;
        case 2:
          book.delete(...);
          break;
        ...
      }

      System.out.println(book.toString());
      ...
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM