【问题标题】:Using arrays to hold indices for other arrays使用数组来保存其他数组的索引
【发布时间】:2014-11-28 22:45:32
【问题描述】:

我正在开发一个需要大量索引用于各种数组的程序。我想,这样做的一种方法是拥有一个包含所有索引的数组。例如:

int[] indices = {1, 2, 3, 4, 5};
char[] chars = {'a', 'b', 'c', 'd', 'e'};

结果是“a”会输出一次,“b”会输出两次,以此类推

最好的方法是什么?

【问题讨论】:

  • 请说明实际问题。这不仅是地图之类的东西会按照您的描述进行操作的情况,而且不是索引。
  • 我只会在你没有其他解决方案时使用数组(或者当数组合适时),地图呢?
  • 好吧,索引是最终结果,这只是达到这一点的过程。我相信我已经彻底解释了这个场景;有什么深奥的吗?基本上它只是使用indexes 数组作为操作chars 数组的决定因素。
  • Java 是一种面向对象的语言。放弃 C 风格的方法,并存储一些包含 char 和 int 字段的 CharCounter 对象的数组。
  • 您要查找的是字典数据结构。

标签: java arrays for-loop indexing iteration


【解决方案1】:

更好的数据结构是地图界面

Map 是将键映射到值的对象。地图不能包含 重复键:每个键最多可以映射到一个值。

地图接口有多种实现方式

  1. HashMap:HashMap 类使用哈希表来实现 Map 接口。这使得 get( ) 和 put( ) 等基本操作的执行时间即使对于大型集合也保持不变。

  2. TreeMap:TreeMap 类使用树实现 Map 接口。 TreeMap 提供了一种按排序顺序存储键/值对的有效方法,并允许快速检索。

  3. LinkedHashMap:该类扩展了 HashMap 并维护映射中条目的链表,按照它们被插入的顺序。 这允许在地图上进行插入顺序迭代。也就是说,在迭代 LinkedHashMap 时,元素将按照插入的顺序返回。

视觉呈现

╔══════════════╦═════════════════════╦═══════════════════╦══════════════════════╗
║   Property   ║       HashMap       ║      TreeMap      ║     LinkedHashMap    ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║              ║  no guarantee order ║ sorted according  ║                      ║
║   Order      ║ will remain constant║ to the natural    ║    insertion-order   ║
║              ║      over time      ║    ordering       ║                      ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║  Get/put     ║                     ║                   ║                      ║
║   remove     ║         O(1)        ║      O(log(n))    ║         O(1)         ║
║ containsKey  ║                     ║                   ║                      ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║              ║                     ║   NavigableMap    ║                      ║
║  Interfaces  ║         Map         ║       Map         ║         Map          ║
║              ║                     ║    SortedMap      ║                      ║
╠══════════════╬═════════════════════╬═══════════════════╬══════════════════════╣
║              ║                     ║                   ║                      ║
║     Null     ║       allowed       ║    only values    ║       allowed        ║
║ values/keys  ║                     ║                   ║                      ║
╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣
║              ║   Fail-fast behavior of an iterator cannot be guaranteed       ║
║   Fail-fast  ║ impossible to make any hard guarantees in the presence of      ║
║   behavior   ║           unsynchronized concurrent modification               ║
╠══════════════╬═════════════════════╦═══════════════════╦══════════════════════╣
║              ║                     ║                   ║                      ║
║Implementation║      buckets        ║   Red-Black Tree  ║    double-linked     ║
║              ║                     ║                   ║       buckets        ║
╠══════════════╬═════════════════════╩═══════════════════╩══════════════════════╣
║      Is      ║                                                                ║
║ synchronized ║              implementation is not synchronized                ║
╚══════════════╩════════════════════════════════════════════════════════════════╝

Read about HashMap

Read about TreeMap

Read about LinkedHashMap

source for visual presentation

【讨论】:

  • 基本上我测试最多的是一堆 for 循环,它们会执行逻辑来完成这个。我不想使用地图的主要原因是因为我不想一遍又一遍地说“mymap.add(...)”,或者让 for 循环为我使用我已经设置的数组中的数据并将所述数据放入地图中。
  • @T145 将信息添加到地图使用 put 函数。你有什么不想使用地图的理由吗?
  • 嗯,这只是“mymap.put(...)”;原因仍然存在。
  • 你愿意分享你的理由吗?
  • 是的;我觉得使用数组并为逻辑使用大约 10 行而不是仅仅使用更多的方式来提供输入会很棒。如果有办法有效地将我的数组转换为地图,那将是可以接受的。
【解决方案2】:

这些天我的很多答案似乎都使​​用HashMap。我觉得它的用途被低估了,特别是因为它在处理相关数据方面非常通用。

HashMap<Char, Integer> foo = new HashMap<Char, Integer>();
foo.add('a', 1);
foo.add('b', 2);
foo.add('c', 3);
foo.add('d', 4);
foo.add('e', 5);

for(Entry<Char, Integer> e : foo.getEntrySet()) {
    for(int i = 0; i < e.getValue(); i++) {
        System.out.print(entry.getKey());
    }
}

当然,这假设您不喜欢订购。如果你这样做了,那么你应该使用ArrayList 和一个存储字符和整数的对象。

创建多个相互依赖的数组是相对不安全的,所以你不妨创建一个对象来为你处理一切。

【讨论】:

  • 您真的需要完全 HashMap吗?如果不是,请将其声明为Map。而且我认为,今天使用菱形语法而不是重复使用&lt;Char, Integer&gt; 是正确的。
【解决方案3】:

由于我相信您确实想按顺序迭代您的列表,因此此处不适合使用 Map。更好的方法是意识到您使用的是面向对象的语言。创建一个对象来为您的数据建模:

class CharCounter {
  private final char toPrint;
  private final int times;
  public CharCounter(final char toPrint, final int times) {
    this.toPrint = toPrint;
    this.times = times;
  }
}

CharCounter[] arr = new CharCounter[]{ new CharCounter('a', 1), new CharCounter('b', 2), ...};
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
  • 2023-03-31
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
相关资源
最近更新 更多