[Java] Difference between array and ArrayList

It only looks the same to beginners ...

** "Array is an array in the first place, isn't it? ArrayList is also an array, isn't it !?" **

I don't think this is just me.

I think that you will understand it empirically as you use it, but if you do not understand it properly, you may be in trouble.

I will make a note of my own understanding, so I hope it will help beginners to imagine a little difference.

What is a class in the first place?

The point is that ** data and processing are handled as a unit **.

I didn't know this at first, and I'm still not sure if it's so useful. .. ..

It is often explained by car, but in the end it was indigestion with ** "I understand what you are saying ..." **.

However, in the end, it was refreshing to think of it as a ** template **.

Use template for class and template for instantiation

Even if a beginner tells me that I can't use it unless I instantiate it into a class, I think it's kind of a mess.

By car

Integrate these things together as a class

Even if it is said that the class will be available by doing so.

Is it possible to instantiate the process of running and operate the stored variables below? I don't understand even if I write it.

However, if you think of it as a template in the first place, it will be refreshing.

Let's make a car! When I thought ** Vehicle type: Devil ** ** Displacement: 100 million ** ** Fuel: Crude oil **

** Good made! Let's sell it! What about the price? ** **

Is that a problem? The person who talks about making a car at the first stage must be a different person at that time. If there is no decision at that time, the first decision will be a problem for each person if it depends on the person.

So, let's make a car class template here Vehicle type Displacement fuel price color speed Run Stop Back Please decide first! By doing so, there is no such thing as "I wasn't able to decide this" or "What was the decision?"

So, according to this template, it is instantiation to decide "Then let's make the car model a devil!"

Strictly different, but I think this understanding is almost okay.

As for the processing, instead of deciding how fast the running process should move for each design, first decide the value of speed and use that speed value, ** "This If you make a template saying "Go forward at about the speed" **, it feels like you can run properly with the same command every time.

Therefore, rather than collecting only the values like an array, if you also summarize the processing of what to do with those values, the description will be unified.

If you think of it as a template, it's convenient.

Array is a class

When retrieving variables in a class Class name.Variable name So, in order to retrieve the number of elements in the array, the number of elements is contained in the variable called length in the array class.

array.length

You can take it out by doing like.

ArrayList stores and encapsulates classes

Variables cannot be accessed directly when encapsulated. Because

array.length

Even if you write like this, it will only result in an error.

Then, what should I do? I use ** methods ** such as getters and setters, which are mentioned in the explanation of encapsulation, to get the value of a variable.

The miso is that you have to use a method.

That is, when getting the number of elements

array.size();

It means that ** () ** is required at the end like. Because it's a method.

Since the writing method is different depending on whether you are using a method or specifying a variable name to get it, rather than remembering that "it works if you write this for the time being", "whether you are using a method or specifying a variable name If you don't know the difference between "or", you may be wondering whether to add ().

It's not very organized, but it's a summary

For the time being, I wrote it with the intention of organizing my head. It's not a strict explanation, but I hope that the vague image will turn into a slightly cleaner image.

Also, I would like to summarize this story in a separate article.

Recommended Posts

[Java] Difference between array and ArrayList
[Java] Difference between == and equals
Difference between ArrayList and LinkedList
Difference between List and ArrayList
[Java] Difference between Hashmap and HashTable
[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 ()
Difference between final and Immutable in Java
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
Difference between vh and%
Difference between i ++ and ++ i
[Java] Understand the difference between List and Set
Difference between next () and nextLine () in Java Scanner
Regarding the difference between Java array and ArrayList, I compared and corresponded methods with similar functions.
[Java] Difference between "final variable" and "immutable object"
Difference between product and variant
Difference between redirect_to and render
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between render and redirect_to
java Generics T and? Difference
Difference between .bashrc and .bash_profile
[Java] Convert ArrayList to array
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
[Java] Difference between static final and final in member variables
[JAVA] What is the difference between interface and abstract? ?? ??
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
[Ruby] Difference between get and post
Difference between instance method and class method
[Java] array
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
Java array
Differences between "beginner" Java and Kotlin
Java array
[Terminal] Difference between irb and pry
java (array)
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Difference between Java and JavaScript (how to find the average)
Java array
[Java] Array
Rough difference between RSpec and minitest
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
Java beginners briefly summarized the behavior of Array and ArrayList
Java basic learning content 2 (array / ArrayList)
[Java] Relationship between H2DB and JDBC