Reputation: 1523
I'm working on a program that requires a lot of indices for various arrays. I thought, that one of the methods for doing so would be to have an array that holds all of the indices. For example:
int[] indices = {1, 2, 3, 4, 5};
char[] chars = {'a', 'b', 'c', 'd', 'e'};
The result being "a" would be outputted once, "b" twice, etc.
What would be the best way to go about doing this?
Upvotes: 1
Views: 59
Reputation: 12766
Since I believe you do want to iterate your list in order, a Map is not suitable here. The better approach is to realize that you're in an object-oriented language; create an object to model your data:
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), ...};
Upvotes: 0
Reputation: 6747
The better Data Structure for your goal is Map interface
A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value.
Map Interface has various implementions:
HashMap : The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.
TreeMap: The TreeMap class implements the Map interface by using a tree. A TreeMap provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval.
LinkedHashMap:This class extends HashMap and maintains a linked list of the entries in the map, in the order in which they were inserted. This allows insertion-order iteration over the map. That is, when iterating a LinkedHashMap, the elements will be returned in the order in which they were inserted.
visual presentation:
╔══════════════╦═════════════════════╦═══════════════════╦══════════════════════╗
║ 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 ║
╚══════════════╩════════════════════════════════════════════════════════════════╝
source for visual presentation
Upvotes: 1
Reputation: 5937
It seems that a lot of my answers use HashMap
these days. I feel like its use is underestimated, specifically because it is so versatile at handling associated data.
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());
}
}
This, of course, assumes you don't have a preference for order. If you do, then instead you should use an ArrayList
and an Object that stores a character and an Integer.
Creating multiple arrays that depend on each other is relatively unsafe, so you might as well create an Object that handles everything for you.
Upvotes: 1