0

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.

public class DatabaseTable extends AbstractTableModel { 

    public ArrayList<Object> objects = new ArrayList<Object>(); 

    public void add(Object o, String sort, String getDesc) { 
        objects.add(o); 
    } 
    // ... 
}

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?

5
  • 2
    Can you show us the code for DatabaseTable.add? Commented Dec 16, 2009 at 11:24
  • Program goes to debug mode when I run the program Commented Dec 16, 2009 at 11:36
  • database tabel = pastebay.com/76967 Commented Dec 16, 2009 at 11:37
  • 1
    It would be helpful for answers that you paste your DatebaseTable sourcecode here as well, and not on some external site. For all other users, the code in question looks like this: public class DatabaseTable extends AbstractTableModel { public ArrayList<Object> objects = new ArrayList<Object>(); public void add(Object o, String sort, String getDesc) { objects.add(o); } // ... } Commented Dec 21, 2009 at 9:27
  • It also would be helpful not call it an array (title). Commented Dec 21, 2009 at 10:40

8 Answers 8

3

It looks like you're passing 4 arguments to a 3 argument function.

Sign up to request clarification or add additional context in comments.

Comments

0

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

yourArray[0] = yourObject1;
yourArray[1] = yourObject2;
//...

Comments

0

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

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.