2

I know this question is asked a lot of times in Stack Overflow but mine is a bit different.

I understand that ArrayList begin at index 1 and Arrays begin at index 0. Am I right?

If not then when I convert my ArrayList to an Array.

Ex: code

List<String> list = new ArrayList<String>();

list.add("India");
list.add("Switzerland");
list.add("Italy");
list.add("France");

String [] countries = list.toArray(new String[list.size()]);

So if I my first comment was right (If it was. Because on the net I find 3/4 people saying array list begins at 0 at 1/4 saying array list begins at 1. I am a beginner so I have no idea, which is correct?

So if ArrayList begin at 1.

So can the Ex: Code be converted to

List<String> list = new ArrayList<String>();

list.add("India");
list.add("Switzerland");
list.add("Italy");
list.add("France");

String [] countries = list.toArray(new String[list.size()-1]);

Pls. do let me know. Thanks!

3
  • The change I made in the second code is that: List<String> list = new ArrayList<String>(); list.add("India"); list.add("Switzerland"); list.add("Italy"); list.add("France"); String [] countries = list.toArray(new String[list.size()-1]); marked in bold Commented Aug 15, 2015 at 14:47
  • This question has no connection to android Commented Aug 15, 2015 at 14:51
  • Ok its related to an app I'm making but if you say so. I'll remove that tag Commented Aug 15, 2015 at 14:52

3 Answers 3

4
String [] countries = list.toArray(new String[list.size()-1])

This line of code will produce an array with countries from given list decreased by one last row.

Both arrays and lists indexing starts with 0 in Java. Every first element in arrays and lists begin with index equal to 0. Every list from JAVA API implements Collection interface and its size() method returns exact number of elements in used collection.

Check out this: Java ArrayList Index

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

10 Comments

In a certain app of mine I asked my ArrayList to display the .size() property. The number of Strings added were 7. So if you are right shouldn't the size be 6. Yet it showed me `.size()' = 7.
Pls do understand I am a beginner so pls do help me understand.
The length of an array with index values 0 through 6 is 7, just like the size of an ArrayList with index values 0 through 6.
@PatriciaShanahan Oh! Thanks a lot for explaining it to me!
@KISHORE_ZE Keep in mind that index of List is for tracking current element that you work on. And size is exact amount of elements inside it.
|
2

ArrayList is like normal Arrays starts at index 0.

You can check using this code.

ArrayList<String> list= new ArrayList<String>();

list.add("one");
list.add("two");
list.add("three");

for (int i = 0; i < list.size(); i++) {
    System.out.println(i);
            System.out.println(list.get(i)); } 

Comments

1

ArrayLists are zero based for indexing, as are all collections in Java, JavaScript, Python, Ruby, C#, R, C...

3 Comments

In a certain app of mine I asked my ArrayList to display the .size() property. The number of Strings added were 7. So if you are right shouldn't the size be 6. Yet it showed me `.size()' = 7. Then why is this happening.
The size property is the number of elements not the highest index.
Thanks for explaining it to me!

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.