How to add elements without specifying the length of the array

This is the first article. I decided to join an IT company as a member of society next year, and I am studying Java by myself. .. I'm a super beginner, so I don't think the code is helpful (it's too dirty and not good).


I looked for something like the ArrayList's ʻadd ()method using an array (such as ʻint []).

There is no choice but to change the length of the array one by one

it seems like. ..

To change the length of the array you need to make a copy of the array using System.arraycopy () For example, when changing from ʻint [3] to ʻint [5]


class Hairetsu {
	public static void main(String args[])
	{
    	int[] a = new int[3]; //Original
    	a[0] = 1;
    	a[1] = 2;
    	a[2] = 3;
		
    	int[] b = new int[5]; //Copy to
    	
    	System.arraycopy( a, 0, b, 0, a.length ); //(Array a,from 0 of a,To array b,from 0 in b,Minutes of all elements of a)copy!
    	
    	for ( int i = 0 ; i < b.length ; i++ ){
    		System.out.println( b[i] );
    	}
    	
	}
}

Execution result


1
2
3
0
0

It becomes the code such as. So, if you apply this and try to make something like the ʻadd ()` method in the list, ...

class HairetsuList {
	
	static int[] list;
	
	public static void add(int a){
		
		int i = list.length;
		int[] copy = new int[i+1]; //Create a large array with 1 element from the beginning
		
		System.arraycopy( list , 0 , copy , 0 , i );
		
		copy[i] = a ;
		list = copy; //The length of the array is different, but there is this. ..
		
	}
}

It looks like this, for the time being a test ...

class Test {
	public static void main(String args[])
	{
		HairetsuList test = new HairetsuList();
		test.list = new int[0];
		
    	test.add(4);
    	test.add(5);
    	test.add(3);
    	
    	System.out.print("{ ");
    	for (int i=0 ; i < test.list.length; i++){
    		System.out.print( test.list[i]+" ");
    	}
    	System.out.print("}");
	}
}

Execution result


{ 4 5 3 }

Aside from the childishness of the code, it was pretty good. You can use a list etc. normally. .. This time, I had to use arrays for corporate training tasks, so I tried my best to think about it, but programming is difficult. ..

Recommended Posts

How to add elements without specifying the length of the array
[Swift] How to get the number of elements in an array (super basic)
How to add the same Indexes in a nested array
How to change the contents of the jar file without decompressing
How to add the delete function
[Swift5] How to get an array and the complement of arrays
How to get the length of an audio file in java
How to add a new hash / array
Compare the elements of an array (Java)
How to sort the List of SelectItem
[Swift] Summary of how to remove elements from an array (personal note)
How to convert an array of Strings to an array of objects with the Stream API
Summary of how to select elements in Selenium
How to find the cause of the Ruby error
Customize how to divide the contents of Recyclerview
How to add sound in the app (swift)
How to get today's day of the week
How to display the result of form input
[Java] How to get the authority of the folder
[Java] How to get the URL of the transition source
How to delete / update the list field of OneToMany
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
How to install the root certificate of Centos7 (Cybertrust)
[Java] How to get the maximum value of HashMap
[Rails] How to change the column name of the table
[SwiftUI] How to specify the abbreviated position of Text
[Android] How to get the setting language of the terminal
[Rails] How to get the contents of strong parameters
How to judge the click of any area of the image
How to download the old version of Apache Tomcat
[Swift] How to get the document ID of Firebase
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
How to use git with the power of jgit in an environment without git commands
How to request by passing an array to the query with HTTP Client of Ruby
How to display the select field of time_select every 30 minutes
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
How to specify an array in the return value / argument of the method in the CORBA IDL file
[For Ruby beginners] Explain how to freely delete array elements!
How to retrieve the hash value in an array in Ruby
4 Add println to the interpreter
How to manage the difference in each environment with yml without increasing the number of RAILS_ENV
How to get the longest information from Twitter as of 12/12/2016
How to change the setting value of Springboot Hikari CP
[Ruby] How to retrieve the contents of a double hash
Let's summarize how to extend the expiration date of Rails
How to output array values without using a for statement
How to solve the problems of Java's three Blocking Queues
How to install JDK 8 on Windows without using the installer
[Rails] How to display the list of posts by category
How to mock some methods of the class under test
How to mark up search keywords regardless of case and without changing the search source word
How to derive the last day of the month in Java
How to check the extension and size of uploaded files
How to initialize Java array
[jsoup] How to get the full amount of a document
How to add HDD to Ubuntu
Update to the latest version without specifying the version with gradle wrapper
How to check the database of apps deployed on Heroku
Try to imitate the idea of a two-dimensional array with a one-dimensional array
[Swift] How to dynamically change the height of the toolbar on the keyboard