Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are allowed to use another int array as a backing array then this code should be enough for you:</p> <pre><code>public class ArrayOfInt{ private final int[] array; public ArrayOfInt(int size) { array=new int[size]; } public int length() { return array.length; } public int get(int index) { return array[index]; } public void put(int index, int value) { array[index]=value; } } </code></pre> <p>But one capability of built in datatype <code>int[]</code> will be missing from it. That is the <code>int[]</code> is iterable and hence the <code>for-each</code> loop can be applied on <code>int[]</code>, but not above class. So to make it iterable, you have to change the above code to this:</p> <pre><code>import java.util.Iterator; public class ArrayOfInt implements Iterable&lt;Integer&gt;{ private final int[] array; public ArrayOfInt(int size) { array=new int[size]; } public int length() { return array.length; } public int get(int index) { return array[index]; } public void put(int index, int value) { array[index]=value; } @Override public Iterator&lt;Integer&gt; iterator() { return new Iterator() { private int current_index; @Override public boolean hasNext() { return current_index&lt;array.length; } @Override public Integer next() { return array[current_index++]; } @Override public void remove() { throw new UnsupportedOperationException("Cannot Resize Underlying Array"); } }; } } </code></pre> <p>As you can see above, we have used <code>Iterator&lt;Integer&gt;</code> instead of <code>Iterator&lt;int&gt;</code> as <code>int</code> is not a reference type, but a primitive type. This will force some autoboxing and unboxing when the program runs. Hence a <code>for-each</code> loop over the class <code>ArrayOfInt</code> will not be as efficient as it is over <code>int[]</code>.</p> <p>But if you are not allowed to use <code>int[]</code> as an underlying array of int, then you have code the class <code>ArrayOfInt</code> in a much different way. First you have to decide what kind of data structure your <code>ArrayOfInt</code> will be.(I would prefer an balanced binary search tree, whose nodes are arranged by keys only once during the construction, where key of each node is it's index. And in that case the <code>iterator()</code> method of the the class <code>ArrayOfInt</code> will return a in-order traversal of the tree.) Then you have to write at least 2 classes for that data structure: (1) <code>ArrayOfInt</code> and (2) <code>NodeOfInt</code>. How those objects of nodes are linked &amp; searched by index, depends on the data structure you chose. All the best.</p> <hr> <p>[This part of answer has been added after asker added some more info]</p> <p>change this piece of code :</p> <pre><code>public int ArrayOfInt(int size) { arr = new int[size]; return size; } </code></pre> <p>to this:</p> <pre><code>public ArrayOfIntegers(int size) { arr = new int[size]; } </code></pre> <p>and change this line of code:</p> <pre><code>ArrayofIntegers d = new ArrayofIntegers(); </code></pre> <p>to this line:</p> <pre><code>ArrayofIntegers d = new ArrayofIntegers(10); // Now your array size is fixed at 10 // For an array of size 15 , use statement: ArrayofIntegers d = new ArrayofIntegers(15); </code></pre> <p>You got a NullPointerException because; in Java, every array (be it <code>int[]</code> or <code>float[][]</code> or <code>String[]</code> or <code>AnyClass[]</code> or <code>SomeClass[][]</code>) is a child-class of <code>class Object</code>. So when a new object your class <code>ArrayOfIntegers</code> was created, an object named <code>arr</code> (inside object named <code>d</code>) was initialized automatically to <code>null</code>. Hence the statement <code>arr[index]=value;</code> that was trying to access <code>arr</code> (a <code>null</code>) threw NullPointerException.</p> <p>[the following lines are not related to your nullpointerexception, but still i am adding them for an efficient code ]</p> <p>your function <code>public void put(int index, int value)</code> should either be this:</p> <pre><code>public void put(int index, int value) { arr[index]=value; System.out.print(arr[index]); } </code></pre> <p>or this:</p> <pre><code>public void put(int index, int value) { System.out.print(arr[index]=value); } </code></pre> <p>but not this:</p> <pre><code>public void put(int index, int value) { arr[index]=value; System.out.print(arr[index]=value); } </code></pre> <p>It creates redundancy(repeated statements) in code.</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. VO
      singulars
      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