Java 基于公共类变量对2个类数组进行排序
在java中,是否可以按字母顺序对具有公共字符串类型变量的两个类数组进行排序?我有一个父类和子类,每个都设置为一个数组,我想根据父类中的变量(如标题)按字母顺序对它们进行排序。我对java相当陌生,想知道是否有什么东西可以在java中实现这一点。提前谢谢你的帮助Java 基于公共类变量对2个类数组进行排序,java,arrays,sorting,Java,Arrays,Sorting,在java中,是否可以按字母顺序对具有公共字符串类型变量的两个类数组进行排序?我有一个父类和子类,每个都设置为一个数组,我想根据父类中的变量(如标题)按字母顺序对它们进行排序。我对java相当陌生,想知道是否有什么东西可以在java中实现这一点。提前谢谢你的帮助 import java.util.Scanner; import java.lang.reflect.Array; import java.text.NumberFormat; import java.util.Locale; //d
import java.util.Scanner;
import java.lang.reflect.Array;
import java.text.NumberFormat;
import java.util.Locale;
//declaration of class Bookstore
public class Bookstore
{
// main method begins execution of program
public static void main (String [] agrs)
{
//declare and set variables
double price = 0.0;
int year = 0;
String isbn = "";
String publisher = "";
String author = "";
String title = "";
String website = "";
double value = 0.0;
double sum = 0;
// create Scanner to obtain user input
Scanner input = new Scanner(System.in);
//create a format for currency
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
//construct the Book object as an array
Book[] bk = new Book[3];
EBook[] ebk = new EBook[2];
//for loop to distinguish between the Book array elements
for (int i = 0; i <bk.length; i++)
bk[i] = new Book (title, price, year, publisher, author, isbn);
//for loop to distinguish between the EBook array elements
for (int i = 0; i <ebk.length; i++)
ebk[i]= new EBook(isbn, price, year, publisher, author, title, website);
//set values for variables in the Book class array
bk[0].setIsbn("9780345917430");
bk[0].setTitle("Lord of the Rings");
bk[0].setAuthor("J. R. R. Tolkien");
bk[0].setYear(1954);
bk[0].setPublisher("Allen & Unwin");
bk[0].setPrice(10.75);
bk[1].setIsbn("0747532699");
bk[1].setTitle("Harry Potter");
bk[1].setAuthor("J. K. Rowling");
bk[1].setYear(1998);
bk[1].setPublisher("Scholastic Press");
bk[1].setPrice(14.12);
//calculate value of Book class
for (int i = 0; i <bk.length; i++)
value += bk[i].getPrice();
//calculate value of Book class
for (int i = 0; i <ebk.length; i++)
sum+= ebk[i].getPrice();
//calculate value of entire inventory
for (int i = 0; i <ebk.length; i++)
value = value + sum - ebk[i].getDiscount();
//display results
for (int i = 0; i <bk.length; i++)
System.out.println(bk[i].toString());
for (int i = 0; i <ebk.length; i++)
System.out.println(ebk[i].toString());
//display total value of inventory
System.out.println("Total inventory: " + nf.format(value));
}
}
class Book
{
//define variables
private String isbn;
private double price;
private String publisher;
private String author;
private String title;
private int year;
//constructor that initializes fields
public Book(String title, double price, int year, String publisher, String author, String isbn)
{
this.isbn = isbn;
this.price = price;
this.publisher = publisher;
this.author = author;
this.title = title;
this.year = year;
}
//empty constructor
public Book()
{
this("", 0.0, 0, "", "", "");
}
//set ISBN
public void setIsbn(String isbn)
{
this.isbn = isbn;
}
//get ISBN
public String getIsbn() {
return this.isbn;
}
//set price
public void setPrice(double price)
{
this.price = price;
}
//get price
public double getPrice()
{
return this.price;
}
//set year
public void setYear(int year)
{
this.year = year;
}
//get year
public int getYear()
{
return this.year;
}
//set publisher
public void setPublisher(String publisher)
{
this.publisher = publisher;
}
//get publisher
public String getPublisher()
{
return this.publisher;
}
//set author
public void setAuthor(String author)
{
this.author = author;
}
//get author
public String getAuthor()
{
return this.author;
}
//set title
public void setTitle(String title)
{
this.title = title;
}
//get title
public String getTitle()
{
return this.title;
}
//display results
public String toString()
{
//create a format for currency
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
return "ISBM number: " + this.isbn + "\n" +
"Title: " + this.title + "\n" +
"Author's name: " + this.author + "\n" +
"Year published: " + this.year + "\n" +
"Publisher's name: " + this.publisher + "\n" +
"Sale price: " + nf.format(this.price) + "\n" +
"\n-----------------------------------------------------------------------\n";
}
}
class EBook extends Book
{
//define variable
private String website;
public EBook(String title, double price, int year, String publisher, String author, String isbn, String website)
{
super(title, price, year, publisher, author, isbn);
this.website = website;
}
//empty constructor
public EBook()
{
this.website = "";
}
//get website
public String getWebsite()
{
return website;
}
//set website
public void setWebsite(String website)
{
this.website = website;
}
//set discount
public double getDiscount()
{
return super.getPrice() * 0.10;
}
@Override
public String toString() {
//create a format for currency
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
return "ISBN number: " + super.getIsbn() + "\n" +
"Title: " + super.getTitle() + "\n" +
"Author's name: " + super.getAuthor() + "\n" +
"Year published: " + super.getYear() + "\n" +
"Publisher's name: " + super.getPublisher() + "\n" +
"Sale price: " + nf.format(super.getPrice()) + "\n" +
"Website: " + this.website + "\n" +
"Discount: " +nf.format(super.getPrice() * 0.10) + "\n" +
"\n-----------------------------------------------------------------------\n";
}
}
import java.util.Scanner;
导入java.lang.reflect.Array;
导入java.text.NumberFormat;
导入java.util.Locale;
//班级申报书
公共类书店
{
//主方法开始执行程序
公共静态void main(字符串[]agrs)
{
//声明和设置变量
双倍价格=0.0;
整年=0;
字符串isbn=“”;
字符串publisher=“”;
字符串author=“”;
字符串标题=”;
字符串网址=”;
双值=0.0;
双和=0;
//创建扫描仪以获取用户输入
扫描仪输入=新扫描仪(System.in);
//创建货币的格式
NumberFormat nf=NumberFormat.getCurrencyInstance(Locale.US);
//将Book对象构造为数组
Book[]bk=新书[3];
EBook[]ebk=新电子书[2];
//for循环以区分Book数组元素
对于(inti=0;i是的),这是可行的。只需尝试为仅为Book类的基类实现可比较的接口
class Book implements Comparable<Book> {
// Your Rest of the code...
// Rest of the code ...
@Override
public int compareTo(Book o) {
if(o == null) {
return 1;
}
if (this == o) {
return 0;
} else {
if(this.title != null && o.title != null) {
return this.title.compareTo(o.title);
} else if(this.title != null) {
return 1;
} else {
return -1;
}
}
}
}
希望它能帮助你:)请提供您的代码片段来解释问题。我用代码编辑了我的原始帖子。这有帮助吗?请检查我发布的答案。如果有帮助,请让我知道。如果它对您有效,请将其标记为接受答案。:)空实体和非空实体不等效。否则,您的答案是正确的。您能解释一下吗?我是just证明一个准则。不是为某人编码。:)希望你会同意我。当然。如果我有一个对象的实例,并且我将它与null
进行比较,我将比较存在的东西与不存在的东西。在比较中的0表示对象等价;也就是说,它们等价地进行比较。I坚定地相信,将任何存在的对象与null
进行比较应该意味着它返回1
而不是0
,因为非null对象明显大于null
对象。恕我直言,这不是它的工作方式。你可以自己尝试一个样本。哦,你甚至没有检查null
。哎呀,我的错。不过,您可能需要在比较中检查null
。
Arrays.sort(bk);