尝试增加数组大小并添加新值

时间:2014-01-09 02:18:54

标签: java arrays

这仅适用于简单的java项目。这是代码。基本上我所要做的就是增加这个数组的大小(如果它已满)然后当用户决定添加一个值时,增加大小并将值添加到数组中。

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++;


}

}

所以基本上当我使用选项添加一个值时,我希望将它添加到最后。

IE中。 1,3,4是原始的。如果我输入6我想要6在1,3,4,6的末尾。现在,如果我输入6则变为1,3,6

2 个答案:

答案 0 :(得分:4)

你的randomize方法没有设置c - 所以当你稍后添加一个新数字时,指针仍然在数组的开头。

顺便提一下,您确实知道您实际上是在这里重新创建ArrayList吗?如果您将此作为学习练习,但是对于任何正确的编程,您应该使用ArrayList

答案 1 :(得分:1)

这是如何将元素添加到数组

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

输出

[1, 2, 3, 4]