Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You are creating an array to hold 0 elements .</p> <pre><code>String s[]=new String[0]; //&lt;&lt; intialized with length 0 </code></pre> <p>It will throw <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/ArrayIndexOutOfBoundsException.html" rel="nofollow noreferrer"><code>ArrayindexoutOfBoundsException</code></a> when you try to access its 1st element <code>s[0]</code>.</p> <blockquote> <p>Thrown to indicate that an array has been accessed with an illegal index. <strong>The index is either negative or greater than or equal to the size of the array.</strong></p> </blockquote> <p>Size the size of your array is <code>0</code> , hence Exception is thrown while accessing index <code>0</code>.</p> <p>Below a basic diagram of an array for understanding.</p> <p><img src="https://i.stack.imgur.com/Gpmn5.gif" alt="enter image description here"></p> <p>It is an array of length <code>10</code> , indexed from <code>0</code> to <code>9</code>.</p> <p>Since you don't know the number of elements your data structure needs to store while declaring the array itself. It is better to use a dynamic <a href="http://docs.oracle.com/javase/tutorial/collections/index.html" rel="nofollow noreferrer">Collection</a> in your case , probably any one of the implementation of <a href="http://docs.oracle.com/javase/7/docs/api/java/util/List.html" rel="nofollow noreferrer">List</a> , like an <a href="http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html" rel="nofollow noreferrer">ArrayList</a>.</p> <pre><code>List&lt;String&gt; s = new ArrayList&lt;String&gt;(); while(rs.next()) { s.add(rs.getString("NAME")); // using column name instead of index "1" here } </code></pre> <p><strong>Suggested Reading:</strong></p> <ol> <li><a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html" rel="nofollow noreferrer">Oracle's tutorial on Java arrays</a></li> <li><a href="https://stackoverflow.com/questions/12321242/lists-vs-arrays-when-to-use-what">Lists vs Arrays - when to use what</a></li> </ol>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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