views:

312

answers:

8

My question is about how I add an object to an array, in my case I have an array class with 4 columns and I cant get my program to add an object to the array.

I have tried with:

DatabaseTable dt = new DatabaseTable();
dt.add("something", "something", "something", "something");

but my program wont run.. Anyone how knows how to do it?

A: 

By add, do you mean assign a value to an array element?

yourArray[0] = yourObject1;
yourArray[1] = yourObject2;
//...
Maximilian Mayerl
A: 

I don't know the class DatabaseTable. Have you tried with ArrayList? Something like:

ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
ArrayList<String> tmp = new ArrayList<String>();
tmp.add("something");
tmp.add("foo");
tmp.add("bar");
tmp.add("wtf");
list.add(tmp);
raffael
A: 

Is DatabaseTable some class you developed to implement an array behaviour? If that's the case, you need to debug the add() implementation. Without context we can't help more.

If you are implementing an array by yourself due to a homework assigment, first you must be aware of the existence and behaviour of the array structure provided by the language. Why your program don't run? It throws an exception?

Check how arrays are covered by The Java Tutorial. The Collections framework also should be checked.

JuanZe
A: 

Depends on what it is backed by. If it is for instance java.util.ArrayList, then you can just use its add() method:

List<String> list = new ArrayList<String>();
list.add("something");
list.add("something");
list.add("something");
list.add("something");

If you want to use varargs in your DatabaseTable#add() method, then it should look like:

public void add(String... strings) {
    list.addAll(Arrays.asList(strings));
}

or a bit more efficient:

public void add(String... strings) {
    for (String string : strings) {
        list.add(strings);
    }
}

this way you can use it as you wanted:

dt.add("something", "something", "something", "something");

But if it is instead backed by a plain String[] array, then you'll need to do a bit more work:

private String[] array = new String[0];

public void add(String... strings) {
    int length = array.length;
    String[] newArray = new String[length + strings.length];
    System.arraycopy(array, 0, newArray, 0, length);
    array = newArray;
    for (int i = 0; i < strings.length; i++) {
        array[length + i] = strings[i];
    }
}

For more about collections (where ArrayList fall in) and arrays you may find the Sun tutorials useful: Trial: Collections and Language basics: Arrays.

BalusC
A: 

You can use something like this:

public Object[] addObjectToArray(Object[] array, Object o)
{
    Object[] ret = new Object[array.length + 1];
    System.arraycopyt(array, 0, ret, 0, array.length);
    ret[array.length] = o;
    return ret;
}
Martijn Courteaux
A: 

It looks like youre passing 4 arguments to a 3 argument function.

Mark E
A: 

In your pastebay code, DatabaseTable.add() has three arguments, and your code above has four.

CPerkins
A: 

You can create array of objects by creating array of particular length and you can put objects on array by position.

public static Object [] addToArray(){
 List list1 = new ArrayList();
 list1.add("11111");

 List list2 = new ArrayList();
 list2.add("2222222222");

 List list3 = new ArrayList();
 list3.add("3333333333");

 List list4 = new ArrayList();
 list4.add("444444444");
 Object []arr = new Object[4];

 arr[0] = list1;
 arr[1] = list2;
 arr[2] = list3;
 arr[3] = list4;
 return arr;

}
Satya