I use Java. when I use ArrayList in Java. if I access index number randomly.
Is this posible?
If this is not posible. How should I do?
For example
ArrayList<String> al = new ArrayList<String>();
al.add(100,"stackoverflow");
Is this posible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What you seem to need isn’t an arrayList but a map between integers and strings :
So you can write
And
Adding onto why you cannot use an arraylist:
When you first initialize an ArrayList, the size is zero. Attempting to add at an index that is larger than the size will still throw an IndexOutOfBounds exception just like an array would. The benefit of the ArrayList is that it will dynamically allocate more memory should the size fill up.
Besides, arrays aren’t efficient for “sparse arrays” even if you make the effort to manage the size to avoid errors.