Tag: 集合

为什么ArrayList中的ConcurrentModificationException?

为什么下面的代码抛出ConcurrentModificationException? Josh Bloch可以避免ConcurrentModificationException。 ArrayList list=new ArrayList(); list.add(100); list.add(200); list.add(300); list.add(400); for(Integer field : list) { list.remove(field); list.add(200); }

如何创建只有一个数组的Object数组列表?

我有以下代码片段,我无法理解为什么不起作用: List listOfObjectArrays; listOfObjectArrays = new ArrayList(); Object[] objectArray = new Object[] {1, “two”, null}; listOfObjectArrays.add(objectArray); // works just fine listOfObjectArrays = Arrays.asList(objectArray, objectArray); // works just fine listOfObjectArrays = Arrays.asList(objectArray); // * // compile error: Incompatible types. Required: List Found: List listOfObjectArrays = Arrays.asList(new Object[] {1, “two”, null}); // compile error: Incompatible types. Required: […]

等于具有可比接口的方法合同

我有像Person这样的自定义类: public class Person { int age; String name; } 现在我想根据age对Person类对象进行排序。 所以我将使用Comparable接口并实现compareTo()方法。 并且比较将有基于age比较人物对象的逻辑。 所以,如果我这样做: Collections.sort(list); // where list is a list of person 我会根据age得到排序人名单。 但我在某处读到,当我们进行Comparable实现时,我们也需要重写equals()方法。 但我现在还没有看到它的使用。 任何人都可以解释,如果我想根据age sort ,还需要覆盖equals()方法吗?

如何对集合中的两个不同对象进行排序?

假设我有两个类CLassA和CLassB。 它们有一个共同点,例如每个类所持有的元素数量。 如何从ClassA和CLassB的对象创建集合并按该属性排序(按降序递增,无关紧要)? 我创建了一个类型的集合,但是当我尝试实现Comparable Interface时,我无法访问该方法(例如,返回nr元素的get)。 我有什么解决方案? 谢谢你的帮助!

如何编写以通用方式获取迭代器或集合的函数?

在过去8年左右的时间里,我几乎一直是Java程序员,最近我又一直在玩C ++。 对于C ++ STL和Java中的迭代器,我遇到了一个问题。 在Java中,您可以编写一个采用如下迭代器的方法: void someMethod(Iterator data) { // … } 您传入一个Iterator ,该方法不需要知道该迭代器的底层集合是什么,这很好。 在C ++中,迭代器没有通用的基类(据我所知)。 我必须写一个这样的函数: void some_function(std::vector::const_iterator data) { // … } 换句话说, some_function知道迭代器是vector的迭代器。 这不好,因为无论迭代器的底层集合是什么,我都希望函数能够工作。 我怎么能用C ++做到这一点? 如果真的不可能,那么在C ++中创建一个以集合作为参数的函数的最佳方法是什么,但是不需要知道集合的确切类型是什么? 附录 谢谢你的回答。 除了答案之外,我在“C ++标准库:教程和参考” (Nicolai M. Josuttis)一书的第7.5段(迭代器特征)中找到了一些很好的信息。 第7.5.1段解释了如何为不同的迭代器类别编写函数的专用版本。

为NavigableMap编写同步的线程安全包装器

java.util.Collections目前提供以下实用程序方法,用于为各种集合接口创建synchronized包装器: synchronizedCollection(Collection c) synchronizedList(List list) synchronizedMap(Map m) synchronizedSet(Set s) synchronizedSortedMap(SortedMap m) synchronizedSortedSet(SortedSet s) 类似地,它还有6个unmodifiedXXX重载。 这里明显的遗漏是NavigableMap的实用方法。 它确实extends SortedMap ,但SortedSet extends Set , Set extends Collection ,而Collections则为SortedSet和Set提供了专用的实用方法。 据推测, NavigableMap是一个有用的抽象,或者它首先不存在,但它没有实用的方法。 所以问题是: Collections没有为NavigableMap提供实用程序方法的具体原因是什么? 你会如何为NavigableMap编写自己的synchronized包装器? 浏览OpenJDK版本的Collections.java的源代码似乎表明这只是一个“机械”过程 通常你可以像这样添加synchronized线程安全function吗? 如果是这样一个机械过程,它可以自动化吗? (Eclipse插件等) 这个代码重复是必要的,还是可以通过不同的OOP设计模式来避免?

选择阶梯式文字游戏的设计方法

我正在尝试构建一个简单的应用程序,完成的程序看起来像这样: 类似梯子的游戏http://img199.imageshack.us/img199/6859/lab9a.jpg 我还必须为此实现两种不同的GUI布局。 现在我正在试图找出执行此任务的最佳方法。 我的教授告诉我介绍有4种状态的Element类: – 空的 – 不可见(在GridLayout中使用) – 第一封信 – 其他信件 我已经考虑过以下解决方案(通过列表我的意思是任何类型的集合): 1.元素是单个字母,每行是Element []。 游戏类将是数组Element []的数组。 我猜这是最愚蠢的方式,validation可能会很麻烦。 2.像以前一样,但Line是元素列表。 游戏是一系列线条。 像之前一样,但Game是一个行列表。 我应该选择哪一个? 或许你有更好的想法? 如果使用哪一个最好的集合?

Java中的迭代器

什么是迭代器和集合? 这两个有关系吗? // the interface definition Interface Iterator { boolean hasNext(); Object next(); // note “one-way” traffic void remove(); } // an example public static void main (String[] args){ ArrayList cars = new ArrayList(); for (int i = 0; i < 12; i++) cars.add (new Car()); Iterator it = cats.iterator(); while (it.hasNext()) System.out.println ((Car)it.next()); } […]

HashSet与JDK 7/8的顺序和区别

这是一个两部分问题: HashSet是否实现了一些隐藏的排序机制,或者只是引用文档: It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. 告诉我,未来有时可能会改变订单和/或取决于内存使用情况? […]

Java排序与MySQL排序不同

我需要检查表中的排序,表格内容由MySQL提供。 我正在使用以下行对列表进行排序: Collections.sort(sorted, String.CASE_INSENSITIVE_ORDER); 并得到以下结果: tes3@test.com test4@test.com test5@test.com test@test.com test_user@mail.com user-og@driver.com 这是我通过查询从MySQL获得的: SELECT ’email’ FROM ‘user’ WHERE 1 ORDER BY ‘user’.’email’ ASC : tes3@test.com test_user@mail.com test@test.com test4@test.com test5@test.com user-og@driver.com 如您所见,订单不一样。 似乎Java根据ASCII表排序: http : //www.asciitable.com/ 4 (52) – @ (64) – _ (95) 但在MySQL结果中,顺序是_ -> @ -> 4 email字段排序规则为: utf8_unicode_ci 有什么问题,是否有任何比较器以相同的方式进行排序?