Java数组,实例化和排序 [英] Java Arrays, Instantiation and sorting

查看:253
本文介绍了Java数组,实例化和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了对于具有与实例,数组做一个Java课程一门功课问题和阵列的数据进行排序。这里有一个问题:

I am completing a homework problem for a Java course that has to do with instantiation, arrays and sorting of array data. Here is the question:

创建一个名为 LibraryBook A类,它包含的字段举行
  用于设置和获取一个 LibraryBook 的标题,作者的方法,和
  页数。将文件保存为LibraryBook.java。

Create a class named LibraryBook that contains fields to hold methods for setting and getting a LibraryBook's title, author, and page count. Save the file as LibraryBook.java.

编写实例5 LibraryBook 对象的应用程序和
  提示您输入的数据字段的值用户。然后提示用户
  进入由场 LibraryBooks 应分类 - 标题,
  作者或页面计数。执行请求的排序程序,
  显示 LibraryBook 的对象。将文件另存为
  LibraryBookSort.java。

Write an application that instantiates five LibraryBook objects and prompts the user for values for the data fields. Then prompt the user to enter which field the LibraryBooks should be sorted by - title, author, or page count. Perform the requested sort procedure and display the LibraryBook objects. Save the file as LibraryBookSort.java.

该教授还增加了以下标准除了书:

The professor also added the following criteria in addition to the book:

声明 LibraryBook 对象的数组,要么通过对它们进行排序
  标题,作者或页数,作为用户请求。

Declare an array of LibraryBook objects and sort them either by title, author or page count, as the user requests.

下面是code我有LibraryBook.java:

Here is the code I have for LibraryBook.java:

public class LibraryBook
{
        String bookTitle;
        String bookAuthor;
        int bookPageCount;

        public LibraryBook(String title, String author, int count)
        {
            bookTitle = title;
            bookAuthor = author;
            bookPageCount = count;
        }

        public String getBookTitle()
        {
            return bookTitle;
        }

        public String getBookAuthor()
        {
            return bookAuthor;
        }

        public int getBookPageCount()
        {
            return bookPageCount;
        }
}

下面是code我至今对LibraryBookSort.java:

Here is the code I have so far for LibraryBookSort.java:

import java.util.Arrays;
import java.util.Scanner;

public class LibraryBookSort
{
    public static void main(String[] args)
    {

    LibraryBook[] book = new LibraryBook[5];

    book[0] = new LibraryBook("Java Programming", "Joyce Farrell", 881);
    book[1] = new LibraryBook("Team Of Rivals", "Dorris Kearns Goodwin", 994);
    book[2] = new LibraryBook("1776", "Daivd McCullough", 400);
    book[3] = new LibraryBook("No Ordinary Time", "Dorris Kearns Goodwin", 768);
    book[4] = new LibraryBook("Steve Jobs", "Walter Isaacson", 656);

    for (int x = 0; x < book.length; ++x)
        book[x].getBookTitle();
    for (int x = 0; x < book.length; ++x)
        System.out.println(book[x].getBookTitle());

    for (int x = 0; x < book.length; ++x)
        book[x].getBookAuthor();
    for (int x = 0; x < book.length; ++x)
        System.out.println(book[x].getBookAuthor());

    for (int x = 0; x < book.length; ++x)
        book[x].getBookPageCount();
    for (int x = 0; x < book.length; ++x)
        System.out.println(book[x].getBookPageCount());
    }
}

上面的code似乎工作并显示所有数据,虽然格式不正确。我希望数据如下所示:

The code above seems to work and displays all of the data, although not formatted correctly. I want the data to look like the following:

Java Programming       Joyce Farrel               881
Team Of Rivals         Dorris Kearns Goodwin      994
1776                   Daivd McCullough           400  
No Ordinary Time       Dorris Kearns Goodwin      768
Steve Jobs             Walter Isaacson            656

在除了输出格式化像上面的,我需要在每个数据类型(标题,作者,页数)由用户选择排序的。

In addition to the output being formatted like the above, I need to have each of the data types (Title, Author, Pages) sortable by the users selection.

在这一点上,我只是失去了。这是我上面的技能水平迄今。我希望有人可以给我在哪里在这一点上走了一些三分球/方向。

At this point, I am just lost. This is above my skill level thus far. I am hoping that someone could give me some pointers/direction on where to go at this point.

推荐答案

关于格式,你只需要一个单一的循环,会显示如下的所有细节:

Regarding the formatting, you just need one single for loop that will display all the details like this:

for (int x = 0; x < book.length; ++x){
    System.out.println(book[x].getBookTitle() + "\t" + book[x].getBookAuthor() + "\t" + book[x].getBookPageCount());
}

这样,你是在一个时间让所有有关每本书的必要信息,在每次一单句输出,由制表符分隔值( \\ t )。

一半你现在正在做正确的循环是没用的,像这样的:

Half the loops you are doing right now are useless, like this one:

for (int x = 0; x < book.length; ++x)
    book[x].getBookTitle();

你没有得到任何变量的称号,所以你只是执行整个阵列白白环路上。

you are not getting the title in any variable, so you are just executing a loop on the whole array for nothing.

有关排序选项,您既可以查找到排序阵列的方法,如果你被允许使用它,或尝试自行实施分类方法,将可能受益于您的阵列和遍历数组的知识。

For the sorting option, you could either look into the sort method of Array if you are allowed to use it, or try to implement a sorting method by yourself which would probably benefit to your knowledge on arrays and iteration over arrays.

这篇关于Java数组,实例化和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆