Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your variable final23 is never initialized. In java you have to initialize an array before using it. Either you do it during the declaration (like you did with k and l) or you have to do it later with a new <code>arrayType[arraySize];</code></p> <hr> <p>Here are the way an array can be declared/initialized.</p> <pre><code>int[] iArray = {1, 2, 3}; //Declaration, Initialization, set values int[] iArray; //Declaration iArray = new int[3]; //Initialization iArray[0] = 1; //Set value int[] iArray; //Declaration iArray = new Array[3]{1, 2, 3}; // Initialization and set values </code></pre> <p>You can of course for the two last sample put the initialization on the same line that the declaration.</p> <hr> <p>Try this (cleaned) code :</p> <pre><code>public class test1 { int[] final23; public int[] sum(int[] x, int[] y) { final23 = new int[Math.min(x.length, y.length)]; for (int i = 0; i &lt; final23.length; i++) { final23[i] = x[i] + y[i]; } return final23; } public void print() { for (int aFinal23 : final23) { System.out.println(aFinal23); } } public static void main(String[] args) { int l[] = {4, 7, 2}; int k[] = {4, 6, 2}; test1 x = new test1(); x.sum(k, l); x.print(); } } </code></pre> <hr> <p><strong>Resources :</strong></p> <ul> <li><a href="http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html" rel="nofollow noreferrer">Oracle.com - Arrays</a></li> <li><a href="http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html#11358" rel="nofollow noreferrer">JLS - Array Initializers</a></li> <li><a href="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#46168" rel="nofollow noreferrer">JLS - Array Creation Expressions</a></li> </ul>
 

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