Java: Expanding array size, can't seem to keep all values in original locations -


For my current homework, I'm trying to sort my array through a normal class because the user Inserts value in places when the size is fully loaded, then the array class calls in an extension method which increases the size of the array, while maintaining its values ​​in the appropriate values, which I have given to my professor Sail from the note Used. For some reason, all my values ​​except the location [0] are either fused or erased from the array. I am leaning that the problem arises from the method of expansion, but I do not know how to fix it.

For example, the initial size is currently set to 5, but the extension method is said to be incremented by 3. Users can input them to 1,2,3,4,5 values ​​completely. But the extension is called when the user returns the new value 6 which produces an array of 1, 6, empty, null, empty, null. Any further error "thread in exception" will lead to the main "java.lang.NullPointerException"

error: Here is my sorted array class:

  public class SortedArray { Private int size; Private Incharge; Private Ent Top; Comparative [] A; Public SourceAdt. (Intermediate Committee, Int Information Amount) {Top = -1; Size = initial size; Increment = increment amount; A = new comparative [size]; } Public int appropriate position (comparison value) {int hold = top; If (hold == -1) {hold = 0; } Else {for (int i = 0; i & lt; = top; i ++) {if (value.compareTo (a [i])> gt; {hold = i + 1;}}} return hold;} Public Comparison The Smallest () {Back One [0];} Public Comparison The Largest () {Return [Top];} Enter the Public Zero (Comparative Value) // The method for which my driver calls {int ap = fair Post (value); // If the extension is full (full) == true) {expansion ();} top position for top (difference i = top; i> = ap; i--) {{A [i + 1] = a [i];}} a [ap] = value; top ++;} abstract Public Bullion Full () (if (top == a.length -1) {Back true;} Other {return false;}} Public zero extension () / / Here is where the extension starts {int newSize = a.length + Increment; comparative [] tempArray = new comparative [new size]; for (int i = 0; i & lt; a.length; i ++) {tempArray [i] = a [i]; a = tmpaarere ;}}  

Here's my driver class that calls for the inserted method in the sorted array class.

  Public class IntDriver {public static Void main [] Args) {Scanner keyboard = new Scanner (System.in); // Creating variable int data; Boolean check = wrong; Int likes; Int size = 5; Int increment = 3; Sorted array B = new sort array (size, increment); // Create the menu system Out.println ("Please select option 1-6."); System.out.println ("1. Insert \ n 2. Delete \ n3. Clear \ n 4. Minimum \ n 5. Biggest \ n6. Exit \ n7. Redisplay Menu"); While (check == incorrect) {choice = keyboard.nextInt (); Switch (Preferred) {Case 1: System.out.println ("Write int data to store in Orange data."); Data = keyboard.nextInt (); Integer insertObj = new integer (data); B.insert (insertObj); System.out.println ("value" + data + "" is inserted); B.print (); break;  

In the extension method, you are replacing a Are very quick The replacement should be after the loop:

  public zero expansion (//) where the extension begins {int newSize = a.length + increment; Comparative [] tempArray = new comparative [new shape]; For (int i = 0; i & lt; a.length; i ++) {tempArray [i] = a [i]; } A = tempArray; }  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -