将多个对象添加到数组中

时间:2013-09-20 23:09:13

标签: java arrays

我想添加刚刚收到的所有用户输入,并将其全部放入一个数组位置。

Scanner sc = new Scanner(System.in);
int cit = 0;

//cit = Integer.parseInt(sc.nextLine());

/*if statement to ensure a value greater than 0 and less than or equal to 50 is
 * entered.
 */
//System.out.println(cit);
System.out.println("Welcome to OswegoNote - your friendly Citation Manager.");
boolean accepted = false;
Index citationIndex;
 Citation currentCitation;
 while (!accepted) {
    System.out.println("How many citations would you like to store today? (between 0 and 50):");
    cit = Integer.parseInt(sc.nextLine());
    int citcounter = cit;
    if (cit <= 50) {

        if (cit > 0) {
            accepted = true;
            citationIndex = new Index(cit);

        } else {
            System.out.println("Error: please enter a number between 0 and 50.");
        }

    } else {
        System.out.println("Error: please enter a number between 0 and 50.");
    }//end if <=50

}//end while loop



for (int i = 0; i < cit; i++) {

    currentCitation = new Citation("");
    currentCitation.updateId(i);

    System.out.println("Please enter publication name (or 'quit' to quit):");
    String name = sc.nextLine();
    currentCitation.setName(name);


    System.out.println("Please enter the date of publication (in mm/dd/yyyy format) (or 'quit' to quit):");
    String pubDate = sc.nextLine();
    currentCitation.setDateOfPublication(pubDate);



    //while (accepted = true) {
    System.out.println("How many authors are there? (max 3):");
    int numAuthors = Integer.parseInt(sc.nextLine());
    //currentCitation.numOfAuthors = numAuthors;
    if (numAuthors <= 3 && numAuthors > 0) {
        for (int a = 0; a < numAuthors; a++) {
            System.out.println("Enter the name of author #" + (a+1) + ". (FirstName MI. LastName) (or 'quit' to quit):");
            String authorName = sc.nextLine();
            currentCitation.addAuthor(authorName);
        }
        accepted = false;
        //break;
    } else {
        System.out.println("Error: Please enter a number between 0 and 3.");
    } //end if
    //}



    System.out.println("Where was it published? (or 'quit' to quit):");
    String pubPlace = sc.nextLine();
    currentCitation.setWherePublisher(pubPlace);

    System.out.println("What is the publisher's name? (or 'quit' to quit):");
    String publisher = sc.nextLine();
    currentCitation.setPublisher(publisher);

    //while (accepted = true) {
    System.out.println("How many keywords would you like to add?(max 5) (or 'quit' to quit):");
    int numKeywords = Integer.parseInt(sc.nextLine());
    if (numKeywords <= 5 && numKeywords > 0) {
        for (int k = 0; k < numKeywords; k++) {
            System.out.println("Enter keyword #" + (k+1) + ". (or 'quit' to quit)");
            String keyW = sc.nextLine();

            currentCitation.addKeyword(keyW);
        }
        accepted = false;
        //break;
    } else {
        System.out.println("Error: Please enter a number between 0 and 5.");
    } //end if
    //}

    System.out.println("This is your Citation:");
    System.out.println("Name: " + currentCitation.getName());
    System.out.print("Author(s): ");

    for (int s = 0; s <= numAuthors - 1; s++) {
        if ((numAuthors - 1) > s) {
        System.out.print(currentCitation.authors[s] + ", ");
    } else {
            System.out.println(currentCitation.authors[s]);
        }
    }
    System.out.println("Date of Publication: " + currentCitation.getDateOfPublication());
    System.out.println("Name of Publisher: " + currentCitation.getPublisher());
    System.out.println("Publication Place: " + currentCitation.getWherePublisher());

    System.out.print("Keywords: ");
    for (int s = 0; s <= numKeywords - 1; s++) {
        if ((numKeywords - 1) > s) {
            System.out.print(currentCitation.keywords[s] + ", ");
        } else {
            System.out.println(currentCitation.keywords[s]);
        }

    }
    System.out.println("Would you like to save this? (yes or no or 'quit' to quit):");
    String answer = sc.nextLine();
    if (answer.equalsIgnoreCase("no")) {

    } else { 
        citationIndex[i] = {currentCitation.authors, currentCitation.keywords, currentCitation.ID, currentCitation.dateOfPublication, currentCitation.name, currentCitation.publisher, currentCitation.wherePublisher};
    }

}// end for

对不起整个主要方法。我不擅长Java,所以我真的不知道如何描述我的情况。

1 个答案:

答案 0 :(得分:0)

使用ArrayList代替数组,然后通过addAll向其中添加多个项目:

如果要添加到其中的项目尚未包含在集合中,请先将它们放在一个:

mylist.addAll(Arrays.asList(1, 2, 3));

或者您可以一次添加一个:

mylist.add(1);
mylist.add(2);
mylist.add(3);

免责声明:我只浏览了您的代码,因为我不想阅读它。我主要根据你的问题标题回答。