Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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!

share|improve this question
    
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 – KISHORE_ZE Aug 15 at 14:47
    
This question has no connection to android – zkminusck Aug 15 at 14:51
    
Ok its related to an app I'm making but if you say so. I'll remove that tag – KISHORE_ZE Aug 15 at 14:52
up vote 3 down vote accepted
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

share|improve this answer
    
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. – KISHORE_ZE Aug 15 at 14:55
    
Pls do understand I am a beginner so pls do help me understand. – KISHORE_ZE Aug 15 at 15:00
    
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. – Patricia Shanahan Aug 15 at 15:00
    
@PatriciaShanahan Oh! Thanks a lot for explaining it to me! – KISHORE_ZE Aug 15 at 15:03
    
@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. – itwasntme Aug 15 at 15:05

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

share|improve this answer
    
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. – KISHORE_ZE Aug 15 at 14:58
1  
The size property is the number of elements not the highest index. – Nick Bailey Aug 15 at 15:04
    
Thanks for explaining it to me! – KISHORE_ZE Aug 15 at 15:05

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)); } 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.