Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use an <code>ArrayList</code> to store the indexes:</p> <pre><code>List&lt;Integer&gt; indexes = new ArrayList&lt;Integer&gt;(); for (int i = 0; i &lt; 1000; i++){ if (sWorld[i].length &gt; 4) { //add i to a list (not an array yet) indexes.add(i); } ... } // then sort the list // not necessary, as indexes are inserted in the right order, but if you must... // Collections.sort(indexes); // and, if you need an array instead of a list Integer[] indexesArray = indexes.toArray(new Integer[indexes.size()]); </code></pre> <p>A <code>List</code>, or an <code>ArrayList</code>, work as variable-length arrays. Though not as efficiently as an actual array.</p> <p>As seen above, there is no need to sort the array later, but, if you must, you can use <code>Collections.sort()</code>.</p> <p>Also, if you must have an <code>int[]</code> instead of an <code>Integer[]</code>, please check: <a href="https://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java">How to convert List&lt;Integer&gt; to int[] in Java?</a></p> <p><strong>Update:</strong></p> <p>As you want to know the size and index of the bigger arrays, it's a whole new problem. Below is a working code that deals with it.</p> <p>Basically, everytime you find an array with size larger than 4, you add a pair <code>(index, size)</code> to the list. This list is then ordered by size, in descending order.</p> <p><strong>At the end of the <code>main()</code> method, an array is created (<code>int[] topTenIndexes</code>) which contains the indexes of the 10 biggest arrays (the indexes are presented in descending order of it's array's length). The result is -1 when there weren't enough big (length > 4) arrays.</strong></p> <pre><code>import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Example { public static void main(String[] args) { List&lt;ArrayIndexAndSize&gt; indexes = new ArrayList&lt;ArrayIndexAndSize&gt;(); int[][] sWorld = {{1},{2,5,5,5,5},{3,6,6,6,6,6}}; for (int i = 0; i &lt; sWorld.length; i++){ if (sWorld[i].length &gt; 4) { // add a pair (index, size) to the list indexes.add(new ArrayIndexAndSize(i, sWorld[i].length)); } //... } // then sort the list by array SIZE, in descending order Collections.sort(indexes); // Print it! System.out.println(indexes); /* output: "[[Array index: 2; Array size: 6], [Array index: 1; Array size: 5]]" */ // Generating an array with the top ten indexes int[] topTenIndexes = new int[10]; Arrays.fill(topTenIndexes, -1); for (int i = 0; i &lt; indexes.size() &amp;&amp; i &lt; 10; i++) { topTenIndexes[i] = indexes.get(i).index; } // Print it System.out.println(Arrays.toString(topTenIndexes)); /* output: [2, 1, -1, -1, -1, -1, -1, -1, -1, -1] */ } public static class ArrayIndexAndSize implements Comparable&lt;ArrayIndexAndSize&gt; { public int index; public int size; public ArrayIndexAndSize(int index, int size) { this.index = index; this.size = size; } /* Order by size, DESC */ /* This is called by Collections.sort and defines the order of two elements */ public int compareTo(ArrayIndexAndSize another) { int thisVal = this.size; int anotherVal = another.size; return -(thisVal&lt;anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1)); } @Override public String toString() { return "[Array index: "+index+"; Array size: "+size+"]"; } } } </code></pre>
 

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