Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is something to get you started:</p> <pre><code>String[] items = new String[] { "A-1", "B-2", "A-5" } // This is the data structure that will receive the final data. The map key is the // group name (e.g. "A" for item "A-15") and the map value is a list of numbers that // have been found for that group. TreeMap is chosen because the groups will be sorted // alphabetically. If you don't need that, you could also use HashMap. Map&lt;String, List&lt;Integer&gt;&gt; groups = new TreeMap&lt;String, List&lt;Integer&gt;&gt;(); for (String item : items) { // Split the item into the group and the number String group = item.substring(0, 1); String number = Integer.toString(item.substring(2)); // See if this group is already registered in our Map List&lt;Integer&gt; groupData = groups.get(group); if (groupData==null) { groupData = new List&lt;Integer&gt;(); groups.put(group, groupData); } // Add the number to the data groupData.add(number); } </code></pre> <p>I assume here that your items are always in the form <strong>1 letter dash number</strong>. If it is a bit more complicated than that, you'll want to have a look at regular expressions (see java class Pattern). This is not tested, I let you test it and handle the special cases.</p> <p>This function will output for <code>{ "A-1", "A-2", "A-3", "B-2", "A-5" }</code>:</p> <pre><code>A -&gt; {1, 2, 3, 5} B -&gt; {2} </code></pre> <p>You'll need to process the resulting number lists if you want to merge consecutive numbers, but that should not be too difficult if you think about it a little while.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload