1

This maybe looks stupid and/or easy question but I couldn't done this.

I'm getting data from database (only). I need get both element and its id at the same time. For example,

+----------------+
|  id | username |
+----------------+
| 1   | user1    |
| 12  | user2    |
| 103 | user3    |
+----------------+

When I populate the ArrayList or ArrayAdapter (or maybe something else), I want to get the both id and username.

I tried to use add(int index, String object) method in ArrayList and insert(String object, int index) method in ArrayAdapter. But the both methods return me same error:

java.lang.IndexOutOfBoundsException: Invalid index 12, size is 1

How can I solve this problem?

Thanks.

2
  • can you post the code ?? Commented Aug 8, 2015 at 18:57
  • I added an answer, something else? Commented Aug 8, 2015 at 19:21

2 Answers 2

2

You used the 12 index, who doesn't exist. If you want add an element to the end, you might use this signature:

objectOfArrayList.add(strigObject); // add an element to end

And, you must always check size of array:

int index = 16;
if (objectOfArrayList.size() > index) {
    objectOfArrayList.add(index -1, stringObject); // add an element to position
}

There was performed checking for adding or inserting value to object of ArrayList:

private void rangeCheckForAdd(int index) {
    if (index > size || index < 0)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

UPDATE:

If you need a structure "key - value" (the idea proposed by Vitaliy Tsvayer), you'll use map:

// id - user
LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
map.put(1, "user1");
map.put(12, "user2");
map.put(103, "user3");

Answer to question in the comments:

LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("user", 1);
int id = map.get("user");
System.out.println("id = " + id); // "id = 1"

It will probably be appeared java.lang.NullPointerException in the last example , if key doesn't exist.

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

4 Comments

Thanks. But if there's only 3 records, then it'll not add the element? I tried it, but not solved my problem. Sorry for late reply...
@MirjalalTalishinski, for these goals used map
@AndrewTobilko you mean look at tsvayer's answer?
@AndrewTobilko so, how can I get this key (id) value for specific user ? For example I want to get 12 if I entered user2 ?
0

the reason that you are getting ArrayIndexOutOfBound exception is that first you are inserting 1 in index 1. but then directly trying to insert 12. but at that time the size of the list is only 1.

my suggestion, use HashMap.. where you store ids as key and username as value i.e.

Map<String, String> = new HashMap <String, String>();
map.put('1', "name 1");

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.