简体   繁体   中英

Trying to increase an array size and add a new value

This is just for a simple java project. Here's the code. Basically all I'm trying to do is increase the size of this array if it is full and then when the user decides to add a value, increase the size and add the value into the array.

public void increaseSize(){

    int[] temp = new int[list.length * 2];

    for (int i = 0; i < list.length; i++){

        temp[i] = list[i];

    }

    list = temp;

}

public void addElement(int newVal){

    if (c == list.length){

        increaseSize();

    }

    list[c] = newVal;
    c++;


}

}

import java.util.Scanner; public class IntegerListTest{

static IntegerList list = new IntegerList(10);

//-------------------------------------------------------
// Create a list, then repeatedly print the menu and do what the
// user asks until they quit
//-------------------------------------------------------
public static void main(String[] args)
{

Scanner input = new Scanner(System.in);
printMenu();
int choice = input.nextInt();
while (choice != 0)
    {
    dispatch(choice);
    printMenu();
    choice = input.nextInt();
    }
}

//-------------------------------------------------------
// Do what the menu item calls for
//-------------------------------------------------------
public static void dispatch(int choice)
{

Scanner input1 = new Scanner(System.in);
int loc;
switch(choice)
    {
    case 0: 
    System.out.println("Bye!");
    break;
    case 1:
    System.out.println("How big should the list be?");
    int size = input1.nextInt();
    list = new IntegerList(size);
    list.randomize();
    break;
    case 2:
    list.selectionSort();
    break;
    case 3:
    System.out.print("Enter the value to look for: ");
    loc = list.search(input1.nextInt());
    if (loc != -1)
        System.out.println("Found at location " + loc);
    else
        System.out.println("Not in list");
    break;
    case 4:
    list.print();
    break;
    case 5:
    System.out.print("# to add: ");
    int newnum;
    newnum = input1.nextInt();
    list.addElement(newnum);
    default:
    System.out.println("Sorry, invalid choice");
    }
}

//-------------------------------------------------------
// Print the user's choices
//-------------------------------------------------------
public static void printMenu()
{

System.out.println("\n   Menu   ");
System.out.println("   ====");
System.out.println("0: Quit");
System.out.println("1: Create a new list (** do this first!! **)");
System.out.println("2: Sort the list using selection sort");
System.out.println("3: Find an element in the list using sequential search");
System.out.println("4: Print the list");
System.out.println("5: Add an element to the list.");
System.out.print("\nEnter your choice: ");
}

}

import java.util.Scanner;

public class IntegerList{

private int c;
int[] list; //values in the list

//-------------------------------------------------------
//create a list of the given size
//-------------------------------------------------------
public IntegerList(int size)
{
list = new int[size];
c = 0;
}


//-------------------------------------------------------
//fill array with integers between 1 and 100, inclusive
//-------------------------------------------------------
public void randomize()
{
for (int i=0; i<list.length; i++)
    list[i] = (int)(Math.random() * 100) + 1;
}

//-------------------------------------------------------
//print array elements with indices
//-------------------------------------------------------
public void print()
{
for (int i=0; i<list.length; i++)
    System.out.println(i + ":\t" + list[i]);
}

//-------------------------------------------------------
//return the index of the first occurrence of target in the list.
//return -1 if target does not appear in the list
//-------------------------------------------------------
public int search(int target)
{
int location = -1;
for (int i=0; i<list.length && location == -1; i++)
    if (list[i] == target)
    location = i;
return location;
}

//-------------------------------------------------------
//sort the list into ascending order using the selection sort algorithm
//-------------------------------------------------------
public void selectionSort()
{
int minIndex;
for (int i=0; i < list.length-1; i++)
    {
    //find smallest element in list starting at location i
    minIndex = i;
    for (int j = i+1; j < list.length; j++)
        if (list[j] < list[minIndex])
            minIndex = j;

    //swap list[i] with smallest element
    int temp = list[i];
    list[i] = list[minIndex];
    list[minIndex] = temp;
    }
}

public void increaseSize(){

    int[] temp = new int[list.length * 2];

    for (int i = 0; i < list.length; i++){

        temp[i] = list[i];

    }

    list = temp;

}

public void addElement(int newVal){

    if (c == list.length){

        increaseSize();

    }

    list[c] = newVal;
    c++;


}

}

So basically when I use the option to add a value I expect it to be added onto the end.

Ie. 1,3,4 is the original. If I input 6 I want 6 to be at the end of 1,3,4,6. Right now if I enter 6 it becomes 1,3,6

Your randomize method doesn't set c - so the pointer is still at the start of the array when you later go to add a new number.

Incidentally you do know that you are essentially recreating ArrayList here right? It's fine if you are doing this as a learning exercise but for any proper programming you should be using ArrayList .

This is how to add an element to array

    int[] a = { 1, 2, 3 };
    a = Arrays.copyOf(a, a.length + 1);
    a[a.length - 1] = 4;
    System.out.println(Arrays.toString(a));

output

[1, 2, 3, 4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM