gpt4 book ai didi

java - Java 中的自定义比较器可以工作,但有一个缺陷

转载 作者:行者123 更新时间:2023-12-01 06:05:21 25 4
gpt4 key购买 nike

我需要编写一个算法,接受用户发送的 10 个项目,无论是字符串还是数字,然后将其放入数组中,我的程序应该对数组进行排序。我不被允许使用Java的方法进行比较或排序。应该是我自己的代码。

我编写的程序运行得很好,它可以很好地对字符串进行排序,也可以很好地对单位数字进行排序。

但是,如果输入两位数,则会将其视为一位数,因为我的程序会查看第一个字符进行比较。例如,1 和 10 将相邻排序。我知道问题是什么,但我不知道如何编写自己的接受通用对象的比较器类。

这是我的代码。

import java.util.Scanner;
public class Main
{

public static void main(String[] args)
{
Object items[] = new Object[10];
Object item;
Scanner scanner = new Scanner(System.in);
SelectionSorter sorter = new SelectionSorter();

System.out.println("Please enter 10 items to be sorted: ");

for (int i = 0; i < items.length; i++)
{
item = scanner.nextLine();

items[i] = item;
}
System.out.println();
System.out.println("Here are the items in ascending order: ");
items = sorter.sortInAscendingOrder(items);
printArray(items);
System.out.println();

System.out.println();
System.out.println("Here are the items in descending order: ");
items= sorter.sortInDescendingOrder(items );
printArray(items);
}

public static void printArray(Object[] items)
{
for (int i = 0; i < items.length - 1; i++)
{
System.out.print(items[i] + ",");
}
System.out.print(items[items.length - 1]);
}
}

public class SelectionSorter
{
Object temp;
Compare compare;

public SelectionSorter()
{

temp = "";
compare = new Compare();
}

public Object[] sortInAscendingOrder(Object[] n)
{
for (int i = 0; i < n.length; i++)
{
for (int j = i; j < n.length; j++)
{
if (compare.compareItems(n[i],n[j]))
{
temp = n[i];
n[i] = n[j];
n[j] = temp;
}
}
}

return n;
}

public Object[] sortInDescendingOrder(Object[] n)
{
for (int i = 0; i < n.length; i++)
{
for (int j = i + 1; j < n.length; j++)
{
if (!compare.compareItems(n[i],n[j]))
{
temp = n[i];
n[i] = n[j];
n[j] = temp;
}
}
}
return n;
}
}

public class Compare
{
int a;
int b;

public Compare()
{
a = b = 0;
}

public boolean compareItems(Object item1, Object item2)
{
for (int i = 0; i < item1.toString().length() && i < item2.toString().length(); i++)
{
a = item1.toString().toLowerCase().charAt(i);
b = item2.toString().toLowerCase().charAt(i);

if (a > b)
{
return true;
} else if (a < b)
{
return false;
}
}
return true;
}
}

最佳答案

您的作业有些不明确:

  1. 用户可以混合数字和字符串吗?
  2. 当你说数字时,这是否包括
    • 负数?
    • 任意长度的数字?
    • 分组分隔符(如 9,100)?
    • 小数分隔符(如 9.32 中所示)?
    • 科学计数法(如 9.34e+7)
    • SI 前缀(如 9K)
    • 十进制以外的数字系统(位置或其他)?
    • 特殊符号,如 ∞ ?

由于这是一项学校作业,我将假设任意长度的非负整数:-)

要提出算法,请考虑如何手动比较数字。最有可能的是,在检查两个数字都没有小数点后,您会比较它们的长度(数字越长越大),如果两个数字同样长,我们可以像字符串一样逐位比较它们。

在代码中,这看起来像这样:

boolean less(String a, String b) {
if (isNumber(a)) {
if (isNumber(b)) {
return numericLess(a,b);
} else {
return true; // number before strings
}
} else {
if (isNumber(b)) {
return false;
} else {
return alphabeticLess(a,b);
}
}
}

boolean numericLess(String a, String b) {
if (a.length < b.length) {
return true;
} else if (a.length > b.length) {
return false;
} else {
return alphabeticLess(a,b);
}
}

关于java - Java 中的自定义比较器可以工作,但有一个缺陷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com