Dynamically increase the number of elements in a Java 2D array (multidimensional array)

At the beginning

When I try to make a two-dimensional array normally in Java, the number of elements of the second element is all constant, but since I found out how to change the number of elements of the second element depending on the first element and other variables. I'll share it!

Definition of a normal two-dimensional array

int[][] arrays = new int[m][n];

Definition of a two-dimensional array whose second element is dynamically determined

//Prepare an array containing the number of elements of the second element (anything is fine)
int[] array = {2, 3, 5};
//Define the number of elements only for the first element
int[][] arrays = new int[array.length][];
for (int i = 0; i < arrays.length; i++){
    //Define the number of elements of the second element while turning the first element with a for statement etc.
    arrays[i] = new int[array[i]];
}

//bonus
System.out.println(arrays.length);
for (int i = 0; i < arrays.length; i++){
    System.out.println(arrays[i].length);
}
//output
//3
//2
//3
//5

Multidimensional array

In the same way

int arrays[][][] =new array [m][n];
//abridgement
arrays[i][j] = new int[array[j]];

You should be able to do it in any dimension! (unconfirmed)

At the end

I think that the array will be more convenient by dynamically changing the number of elements in this way! !! If you have any mistakes, feel free to comment! !!

Recommended Posts

Dynamically increase the number of elements in a Java 2D array (multidimensional array)
Compare the elements of an array (Java)
Measure the size of a folder in Java
[Java] [For beginners] How to insert elements directly in a 2D array
[Swift] How to get the number of elements in an array (super basic)
Summarize the additional elements of the Optional class in Java 9
A quick explanation of the five types of static in Java
Count the number of digits after the decimal point in Java
A program that counts the number of words in a List
Find the number of days in a month with Kotlin
A program (Java) that outputs the sum of odd and even numbers in an array
Cast an array of Strings to a List of Integers in Java
Get the public URL of a private Flickr file in Java
Let's create a TODO application in Java 5 Switch the display of TODO
Extract elements of array / extract in reverse order-java
When seeking multiple in a Java array
Get the result of POST in Java
Examine the memory usage of Java elements
Organized memo in the head (Java --Array)
[Java beginner] About initialization of multidimensional array
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
The story of forgetting to close a file in Java and failing
How to find the total number of pages when paging in Java
Sample program that returns the hash value of a file in Java
How to get the absolute path of a directory running in Java
A fix to prevent the increase in the number of DB connections in the custom authentication provider of the Cognos SDK sample
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
A story about the JDK in the Java 11 era
The story of learning Java in the first programming
Feel the passage of time even in Java
A quick review of Java learned in class
Validate the identity token of a user authenticated with AWS Cognito in Java
Import files of the same hierarchy in Java
Java Welcome to the Swamp of 2D Arrays
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
[Java] Integer information of characters in a text file acquired by the read () method
Get the URL of the HTTP redirect destination in Java
A quick review of Java learned in class part4
[Ruby] The role of subscripts in learning elements in arrays
Examine the elements in the array using the [Ruby] includes? Method
Calculate the difference between numbers in a Ruby array
Awesome Java: A number of great Java framework library software
A note for Initializing Fields in the Java tutorial
[Java] Get the file in the jar regardless of the environment
[Java] When writing the source ... A memorandum of understanding ①
A quick review of Java learned in class part3
Set the maximum number of characters in UITextField in RxSwift
How to convert a file to a byte array in Java
A quick review of Java learned in class part2
Change the storage quality of JPEG images in Java
I got stuck in a clone of a two-dimensional array
A survey of the Kubernetes native Java framework Quarkus
Multidimensional array in Swift
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
[Ruby] Count an even number in an array using the even? Method
[Note] Java Output of the sum of odd and even elements
Declaration of multidimensional array Let's try TypeScript for Java problem 4-4