Regarding the difference between Java array and ArrayList, I compared and corresponded methods with similar functions.

Introduction

When solving a Java problem, I was told to "use an array" as a condition for creating a program. "Well, is it Akan in ArrayList?" "What was an array in the first place, I only recognize it very vaguely." I felt that, so I investigated various things. The experience of touching multiple languages has an adverse effect, and all keywords such as "list", "tuple", "array", and "ArrayList" are mixed up. Even if it is limited to Java, there is still a Linked List, and it is quite difficult to organize.

I felt that it was difficult for me to grasp it comprehensively and deeply, and it was also difficult for me to summarize it in an article. I tried to make it feel fluffy and say, "I'm not wrong for the time being." I don't feel like I can understand and explain generics and implementations yet ...

Execution environment

Paiza.io : openjdk version "15" 2020-09-15)

What you can see in this article

--Arrays and ArrayLists are different like this --"Write this in an array" vs "Write this in an ArrayList" --Declaration --Element assignment --Add element --Getting elements --Getting the number of elements (size)

Difference between array and ArrayList

Differences from the characteristics of arrays and ArrayList

I researched various things, but anyway, the more I dig, the more information I get, and The content is also rich, so it's easy to keep.

The array is fixed length. The number of elements is fixed when declared and cannot be changed. For example, if you say "Let's set this to contain 5 elements!", You cannot insert the 6th element.

ArrayList, on the other hand, is variable length. For the time being, I set the initial number of elements (or it is set by default even if I do not specify it) Even if the number of elements is exceeded, it is possible to throw in elements one after another.

Supplement

The size can be changed, but it will create an ArrayList that can store 10 elements as the initial size. Don't worry too much about this size as it will automatically grow if you run out of it. Let's programming easy-to-understand site even for beginners

About the declaration

Array declaration

Create an int type array called hoge and put an integer from 1 to 5 in it. When I looked it up for the time being, I found three types of hits, so I will list them.

        int[] hoge;
        hoge = new int[5];
        hoge[0] = 1;
        hoge[1] = 2;
        hoge[2] = 3;
        hoge[3] = 4;
        hoge[4] = 5;

int[] hoge = new int {1, 2, 3, 4, 5}
int [] hoge = {1, 2, 3, 4, 5}

ArrayList declaration

Create an Integer type ArrayList called hoge and enter an integer from 1 to 5. When I looked it up for the time being, I found three types of hits, so I will list them. Integer? Isn't it int? If you think You may be happy to search around "Java Primitive Wrapper Class".

ArrayList<Integer> hoge = new ArrayList<Integer>();
hoge.add(1);
hoge.add(2);
hoge.add(3);
hoge.add(4);
hoge.add(5);

//As another way of writing
// List<Integer> hoge = new ArrayList<Integer>();
//Or if Java 10 or later
// var hoge = new ArrayList<Integer>();
//You can also write

List<Integer> list = new ArrayList<Integer>(){
{
    add(1);
    add(2);
    add(3);
    add(4);
    add(5);
             };
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
//You can write it like this, but be careful as it will not be variable length.

About element assignment

As an example, assign 5 to the 0th of an array called hoge (ArrayList).

Element assignment (in the case of an array)

hoge[0] = 5;

Element assignment (for ArrayList)

hoge.set(0, 5)

Add element

Add an int value of 10 as an example

Add element (when array)

Since it has a fixed length, it cannot be done in the first place.

Add element (when using ArrayList)

hoge.add(10);

Get the element

As an example, take out the 0th element of an array (ArrayList). ** Please note that the types of parentheses are different between Array and ArrayList! ** **

Get the element (when it is an array)

int foo = hoge[0];

Get elements (when using ArrayList)

int foo hoge.get(0);

Get the number of elements (size)

Get the number of elements (in the case of an array)

int foo = hoge.length

Get the number of elements (when using ArrayList)

int foo = hoge.size();

in conclusion

I tried to deal with the operations that I thought I would use frequently in the future. I may add it.

reference

--Introduction to Java for a refreshing 3rd edition

Recommended Posts

Regarding the difference between Java array and ArrayList, I compared and corresponded methods with similar functions.
[Java] Difference between array and ArrayList
[Java] Check the difference between orElse and orElseGet with IntStream
Think about the differences between functions and methods (in Java)
[Java] Understand the difference between List and Set
I compared the characteristics of Java and .NET
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Difference between i ++ and ++ i
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources
[JAVA] What is the difference between interface and abstract? ?? ??
[Rails] I investigated the difference between redirect_to and render.
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
[Java] The difference between the Stream.of () and Arrays.stream () methods that you do not know unexpectedly
[Java] ArrayList → Should I specify the size in array conversion?
[Java] Difference between == and equals
Difference between Java and JavaScript (how to find the average)
Java beginners briefly summarized the behavior of Array and ArrayList
Difference between ArrayList and LinkedList
Difference between List and ArrayList
I want to implement various functions with kotlin and java!
The difference between programming with Ruby classes and programming without it
I want to return to the previous screen with kotlin and java!
I tried to summarize the methods of Java String and StringBuilder
[Java] What is the difference between form, entity and dto? [Bean]
I compared PHP and Java constructors
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
I compared Java and Ruby's FizzBuzz.
Interpret the relationship between Java methods and arguments into a biochemical formula
[Rails] I studied the difference between new method, save method, build method and create method.
Understand the difference between int and Integer and BigInteger in java and float and double
Difference between final and Immutable in Java
[For beginners] Difference between Java and Kotlin
About the difference between irb and pry
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
Regarding the transient modifier and serialization in Java
[iOS] Understand the difference between frame and bounds
[Rails / ActiveRecord] About the difference between create and create!
Difference between next () and nextLine () in Java Scanner
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Summarize the differences between C # and Java writing
[Java] Difference between "final variable" and "immutable object"
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
[Ruby] Difference between get and post
Get the result of POST in Java