gpt4 book ai didi

java - 转换后的数组可以与另一个转换后的数组进行比较吗?

转载 作者:行者123 更新时间:2023-12-02 07:12:34 25 4
gpt4 key购买 nike

好吧,我试图正确地表达问题,以便获得我需要的帮助。我得到的是一个简单的钱包/硬币程序,它将字符串数组与另一个字符串数组进行比较。请原谅冗长的循环/嵌套循环/逻辑,因为在此作业中我无法使用 java 数组和集合类中的方法。这是一项类作业,因此请解释一下过程,而不仅仅是回答。

(理论):我认为我对两个转换后的数组的比较是问题的原因,但我无法找到一种方法来将数组列表中的每个元素与另一个数组列表中的每个元素进行比较.

钱包.class:

import java.util.ArrayList;

/**
* A purse holds a collection of coins.
*/
public class Purse
{
private ArrayList<String> coins;

/**
* Constructs an empty purse.
*/
public Purse()
{
coins = new ArrayList<String>();
}

/**
* Add a coin to the purse.
*
* @param coinName
* the coin to add
*/
public void addCoin(String coinName)
{
coins.add(coinName);
}

/**
* Returns a string describing the object.
*
* @return a string in the format "Purse[coinName1,coinName2,...]"
*/
public String toString()
{
String coinName1 = "Quarter";
String coinName2 = "Dime";
String coinName3 = "Nickel";
String coinName4 = "Penny";

String str = "Actual:"
+ "Purse["
+ (coinName1 + "," + coinName2 + "," + coinName3 + "," + coinName2)
+ "]";

return str;
}

/**
* Determines if a purse has the same coins in the same or different order
* as another purse.
*
* @param other
* the other purse
* @return true if the two purses have the same coins in the same or
* different order, false otherwise
*/
public boolean sameCoins(Purse other)
{
if (this.coins.size() != other.coins.size())
{
System.out.println("1");
return false;
}


int matched = 0;
for (int i = 0; i < this.coins.size(); i++)
{
for (int j = 0; j < other.coins.size(); j++)
{
if (this.coins.toArray() == other.coins.toArray())
{
matched++;
System.out.println("2");
System.out.println(this.coins.toArray());
System.out.println(other.coins.toArray());
break;
}
}
}
return matched == this.coins.size();

}

}

PurseTester.class:

/**
* This class tests the Purse class.
*/
public class PurseTester
{
public static void main(String[] args)
{
Purse p = new Purse();
p.addCoin("Quarter");
p.addCoin("Dime");
p.addCoin("Nickel");
p.addCoin("Dime");

System.out.println(p.toString());
System.out.println("Expected: Purse[Quarter,Dime,Nickel,Dime]");

Purse a = new Purse();
a.addCoin("Quarter");
a.addCoin("Dime");
a.addCoin("Nickel");
a.addCoin("Dime");

Purse b = new Purse();
b.addCoin("Nickel");
b.addCoin("Dime");
b.addCoin("Dime");
b.addCoin("Quarter");

System.out.println(a.sameCoins(b));
System.out.println("Expected: true");

Purse c = new Purse();
c.addCoin("Quarter");
c.addCoin("Penny");
c.addCoin("Nickel");
c.addCoin("Dime");

Purse d = new Purse();
d.addCoin("Nickel");
d.addCoin("Dime");
d.addCoin("Dime");
d.addCoin("Quarter");

System.out.println(c.sameCoins(d));
System.out.println("Expected: false");

}
}

输出是:

Actual:Purse[Quarter,Dime,Nickel,Dime]
Expected: Purse[Quarter,Dime,Nickel,Dime]
false
Expected: true
false
Expected: false

预期输出:

Actual:Purse[Quarter,Dime,Nickel,Dime]
Expected: Purse[Quarter,Dime,Nickel,Dime]
true
Expected: true
false
Expected: false

最佳答案

你的循环永远不会查看List,你只是重复检查两个数组的==。您需要比较元素,例如:

    for (int i = 0; i < this.coins.size(); i++)
{
if (!this.coins.get(i).equals(other.coins.get(i)))
{
return false;
}
}

但是,假设您想要相同的订单,则可以进行比较。如果您需要比较忽略顺序,则需要循环遍历另一个数组并在找到该元素时删除该元素,否则返回 false

    final List<String> copy = new ArrayList<String>(other.coins);
outerLoop:
for (final String mine : coins)
{
final Iterator<String> otherIter = copy.iterator();
while (otherIter.hasNext())
{
if (mine.equals(otherIter.next()))
{
otherIter.remove();
continue outerLoop;
}
}
return false;
}

如果您先对 List 进行排序,然后使用第一个最佳方法,那么这种方法显然效率非常低。但鉴于您不能使用 Array 方法,我假设您不能使用 TreeSet 或 Collections.sort() 。

关于java - 转换后的数组可以与另一个转换后的数组进行比较吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15328431/

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