【问题标题】:isPalindrome - Collection and List ReversalisPalindrome - 集合和列表反转
【发布时间】:2016-09-27 15:36:42
【问题描述】:

这是学校的家庭作业实验室。我正在尝试反转 LinkedList,并检查它是否是回文(前后相同)。我在网上看到了类似的问题,但对我有帮助的并不多。我以前做过检查回文的程序,但没有一个检查数组或列表的程序。所以,首先,这是我的 isPalindrome 方法:

public static <E> boolean isPalindrome(Collection<E> c) {
    Collection<E> tmp = c;
    System.out.println(tmp);
    Collections.reverse((List<E>) c);
    System.out.println(c);
    if(tmp == c) { return true; } else { return false; }
}

我的教授希望我们将方法设置为接受所有集合,这就是我使用 Collection 并将其转换为反向方法的列表的原因,但我不确定这样做是否正确。我知道它确实颠倒了列表。这是我的主要方法:

public static void main(String...strings) {
    Integer[] arr2 = {1,3,1,1,2};
    LinkedList<Integer> ll2 = new LinkedList<Integer>(Arrays.asList(arr2));
    if(isPalindrome(ll2)) { System.out.println("Successful!"); }
}

问题是,我正在使用一个不是回文的数组来测试它,这意味着它向后与向前不同。我已经使用数组 {1,3,1} 对其进行了测试,它工作正常,因为这是一个回文。对于回文,使用 {1,3,1,1,2} 仍然返回 true,但显然不是。这是我使用 {1,3,1,1,2} 数组的输出:

[1, 3, 1, 1, 2]
[2, 1, 1, 3, 1]
Successful!

所以,它似乎正确地反转了列表,但是当它比较它们时,它假设它们是相等的?我相信 tmp == c 以及它如何检查它们是否相等存在问题。我假设它只是检查它是否包含相同的元素,但我不确定。我也尝试了 tmp.equals(c),但它返回了相同的结果。我只是好奇是否有另一种方法可以使用,或者我必须编写一个方法来比较 tmp 和 c?

提前感谢您! 汤米

【问题讨论】:

  • 首先,如果Collection c 不是List,那么当您使用(List&lt;E&gt;) c 时,您将拥有ClassCastException
  • @cheb1k4 有没有办法设置它,以便 Collections.reverse() 接受一个集合?
  • 不,你不能,Collections.reverse 只接受List。你必须找到另一种方式。也许你可以用docs.oracle.com/javase/7/docs/api/java/util/…做点什么

标签: java arrays collections linked-list palindrome


【解决方案1】:

在您的代码中,ctmp 是指向同一集合的链接,tmp == c 将始终为真。您必须将您的收藏克隆到新实例,例如:List&lt;E&gt; tmp = new ArrayList(c);

【讨论】:

  • 谢谢!我使用 LinkedList 而不是 ArrayList,但它可以工作,谢谢。
【解决方案2】:

许多小点

public static <E> boolean isPalindrome(Collection<E> c) {
    List<E> list = new ArrayList<>(c);
    System.out.println(list);
    Collections.reverse(list);
    System.out.println(list);
    return list.equals(new ArrayList<E>(c));
}

Reverse 仅适用于有序列表。 制作收藏品的副本。 使用equals 比较集合。

public static void main(String...strings) {
    int[] arr2 = {1, 3, 1, 1, 2};
    //List<Integer> ll2 = new LinkedList<>(Arrays.asList(arr2));
    List<Integer> ll2 = Arrays.asList(arr2);
    if (isPalindrome(ll2)) { System.out.println("Successful!"); }
}

【讨论】:

    【解决方案3】:

    您需要将Collection 复制到List / 数组中。必须这样做,因为为 Collection 定义的唯一顺序是迭代器之一。

    Object[] asArray = c.toArray();
    

    你可以应用你选择的算法来检查这个数组是否是回文来检查,如果Collection是回文。

    或者使用LinkedList,如果列表是回文而不创建新的List来反转,检查会更有效:

    public static <E> boolean isPalindrome(Collection<E> c) {
        List<E> list = new LinkedList<>(c);
        Iterator<E> startIterator = list.iterator();
        ListIterator<E> endIterator = list.listIterator(list.size());
    
        for (int i = list.size() / 2; i > 0; i--) {
            if (!Objects.equals(startIterator.next(), endIterator.previous())) {
                return false;
            }
        }
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 2013-02-03
      • 1970-01-01
      • 2017-01-28
      • 2011-01-29
      • 2011-12-25
      相关资源
      最近更新 更多