简体   繁体   English

数组? 或不?

[英]Arrays? or not?

i am not an expert in java, but i need to solve this problem/activity for my course subject, that's why i really need your help guys. 我不是Java方面的专家,但是我需要为我的课程主题解决此问题/活动,所以我才真正需要您的帮助。 I have a programming problem. 我有编程问题。 thing is i can't figure out what method or java codes should i use for this problem: 问题是我不知道应该使用什么方法或Java代码来解决此问题:

Create a class address book that can contain 100 entries of Name, Address, contact number and email address. 创建一个班级通讯录,其中可以包含100个姓名,地址,联系电话和电子邮件地址条目。 You should provide the following methods for the address book: 您应该为通讯簿提供以下方法:

Add entry, Delete entry, View all entries and Update an entry 添加条目,删除条目,查看所有条目和更新条目

UPDATE: this is the codes I got so far 更新:这是我到目前为止得到的代码

I am thinking i could use 2d array for this but, as soon as i start coding i can't really continue further , I don't know if its possible to use array or not in this kind of activity. 我想我可以为此使用2d数组 ,但是,一旦我开始编码, 我就无法真正继续下去 ,我不知道是否可以在这种活动中使用数组。 I tried searching for other java codes but the more I learned new techniques or codes that might be possible, the more i got confused on WHAT codes must I use! 我尝试搜索其他Java代码,但是我越了解可能的新技术或代码,就越会混淆我必须使用的代码!

if anyone can help me build the coding for this activity I would really apprecaite and will surely study how the hell will/can YOU do it! 如果有人可以帮助我为该活动构建代码,我将非常感谢并一定会研究地狱将如何/您能做到这一点! because im really interested in learning Java, I just need some help to realize how should i DO this. 因为我对学习Java真的很感兴趣,所以我只需要一些帮助就可以实现该目的。 thanks in advance! 提前致谢!

THIS ARE THE CODES I'VE GOT SO FAR: the capability of these program is only for adding editing viewing and deleting NAMES, iam still figuring out how to add dimensions to my array or should I add? 这就是我想知道的代码:这些程序的功能仅是用于添加编辑视图和删除名称,iam仍在弄清楚如何在数组中添加维,还是应该添加? or if not array? 还是如果不是数组? HoW? 怎么样? how am I supposed to answer this activity prior tto its REQUIREMENTS :( 在该活动达到要求之前,我该如何回答:(

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);
}

} }

As you have several entries to store my suggestion is a linked list. 由于您有几个条目要存储,因此我的建议是一个链表。 Linked list is a data structure where you can store multiple entries.Java provide a 链表是一种数据结构,您可以在其中存储多个条目。Java提供了一个

http://www.mycstutorials.com/articles/data_structures/linkedlists this link will help you with linked list. http://www.mycstutorials.com/articles/data_structures/linkedlists此链接将帮助您获取链接列表。

First, implement all the required classes, two in your case: 首先,实现所有必需的类,在您的情况下,两个:

  • AddressBook 地址簿
  • Entry 条目

The skeleton could be something like that 骨架可能是这样的

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;
  }
  ...
}

In order to implement viewAll() you could choose to override toString() methods in both classes, to delete() it seemes that you have to implement find() as well etc. Then just use these classes 为了实现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