Ciara
Ciara

Reputation: 197

How do I add an element to the end of my array?

I don't know if this is right, so I need your comments guys. I have an array of employee names. It will be displayed on the console, then will prompt if the user wants to insert another name. The name should be added on the end of the array(index 4) and will display again the array but with the new name already added. How do I do that? Btw, here's my code. And I'm stuck. I don't even know if writing the null there is valid.

public static void list() {
    String[] employees = new String[5];
    employees[0] = "egay";
    employees[1] = "ciara";
    employees[2] = "alura";
    employees[3] = "flora";
    employees[4] = null;

    for(int i = 0; i < employees.length; i++) {
        System.out.println(employees[i]);
    }
}
public static void toDo() {
    Scanner input = new Scanner(System.in);
    System.out.println("What do you want to do?");
    System.out.println("1 Insert");
    int choice = input.nextInt();

    if(choice == 1) {
        System.out.print("Enter name: ");
        String name = input.nextLine();

Upvotes: 1

Views: 308

Answers (5)

Tushar Paliwal
Tushar Paliwal

Reputation: 301

Here you are using static array which is fixed at the time of creation.I think you should use java.util.Arraylist which will provide you facility of dynamic array.

Upvotes: 0

tygerpatch
tygerpatch

Reputation: 55

It depends on whether the user can enter more than 4 employee names. If they can then using ArrayList is the better choice. Also the employees variable needs to be a static property of your class since being used in a static method.

private static String[] employees = new String[5];

static {
    employees[0] = "egay";
    employees[1] = "ciara";
    employees[2] = "alura";
    employees[3] = "flora";
    employees[4] = null;
}

public static void list() {
    for(int i = 0; i < employees.length; i++) {
        System.out.println(employees[i]);
    }
}

public static void addEmployeeName(String name, int index) {
    employees[index] = name;
}

Upvotes: 0

Dan D.
Dan D.

Reputation: 32391

If you really have to use arrays then you will have to increase the size of the array by using an intermediate copy.

String[] array = new String[employees.length + 1];
System.arraycopy(employees, 0, array, 0, employees.length);
array[employees.length] = newName;
employees = array;

However, the best way would be to use a List implementation.

Upvotes: 1

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

Arrays are fixed in size. Once you declare you can not modify it's size.

Use Collection java.util.List or java.util.Set. Example ArrayList which is dynamic grow-able and backed by array.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500375

You can't, basically.

Arrays have a fixed size when they've been constructed. You could create a new array with the required size, copy all the existing elements into it, then the new element... or you could use a List<String> implementation instead, such as ArrayList<String>. I'd strongly advise the latter approach.

I suggest you read the collections tutorial to learn more about the various collections available in Java.

Also note that you've currently just got a local variable in the list method. You'll probably want a field instead. Ideally an instance field (e.g. in a class called Company or something similar) - but if you're just experimenting, you could use a static field at the moment. Static fields represent global state and are generally a bad idea for mutable values, but it looks like at the moment all your methods are static too...

Upvotes: 5

Related Questions