About Java arrays

Java array

Now that you've learned about Java arrays, we'll output the benefits of arrays and usage examples.

The inconvenience of variables and the benefits of arrays

This time, we will compare how to use variables and arrays using a score management program, and explain the merits of using arrays.

Program using variables

test


public class Main {
  public static void main(String[] args) {
    int sansu = 20;
    int kokugo = 30;
    int rika = 40;
    int eigo = 50;
    int syakai = 80;
    int sum = sansu + kokugo + rika + eigo + syakai;
    int avg = sum / 5;
    System.out.println("Total score:" + sum);
    System.out.println("Average score:" + avg); 
  }
}

Execution result

Total score: 220
Average score: 44

This is not a problem, but there are two inconveniences.

—— As the number of subjects increases, the code becomes cumbersome and redundant. --Cannot process all at once

Program using arrays

test


public class Main {
  public static void main(String[] args) {
    int[] scores = {20, 30, 40, 50, 80};
    int sum = scores[0] + scores[1] + scores[2] + scores[3] + scores[4];
    int avg = sum / scores.length;
    System.out.println("Total score:" + sum);
    System.out.println("Average score:" + avg);
  }
}

Execution result

Total score: 220
Average score: 44

The execution results are the same for both, but using an array makes the code cleaner, and it's a little better. I understand the merits of using arrays like this (?) I will explain each one.

What is an array?

Data that stores multiple data of the same type ** in ** sort order **. In addition, the order of arrangement is called a subscript, and it is a rule that starts from ** 0 **. In a program using arrays, 20 points are the 0th element.

How to write an array

2 Steps are required to create an array.

Step1 Declaration of array

Element type [] Array variable name

int[] scores;

Step2 Creating and assigning elements

scores = new int[5];

new is called the new operator, and you can create the specified number of elements in [].

Step1 and Step2 can be performed at the same time

int[] scores = new int[5];

Get the number of elements in an array

int num = scores.length;

Recommended Posts

[Java] About arrays
About Java arrays
Java, about 2D arrays
About Java interface
[Java] About Java 12 features
Something about java
Where about java
About Java features
About Ruby arrays
About Java threads
[Java] About interface
About Java class
About java inheritance
About interface, java interface
About List [Java]
About java var
About Java literals
About Java commands
About Java log output
About Java functional interface
About class division (Java)
Combine arrays in Java
About Java StringBuilder class
[Java] About Singleton Class
About Java method binding
About method splitting (Java)
[Java Silver] About initialization
About Java Array List
About Java Polymorphism super ()
About inheritance (Java Silver)
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
[Java] About enum type
All about Java programming
About java abstract class
A note about Java GC
About an instance of java
Study java arrays, lists, maps
What I researched about Java 6
[Gradle] About Java plug-in tasks
About Java variable declaration statements
What I researched about Java 9
[Java] About try-catch exception handling
About Java class loader types
[Java Silver] About equals method
[Java] About String and StringBuilder
Java
What I researched about Java 7
About =
About Alibaba Java Coding Guidelines
About Java class variables class methods
About Java Packages and imports
About abstract classes in java
Java
[Android / Java] Learned about DataBinding
What I researched about Java 5
About Java static and non-static methods
How about TECH ACADEMY ?? [Java course]
[Introduction to Java] About lambda expressions