[Small story] I tried to make the java ArrayList a little more convenient

0. (Review the article yourself)

It's a boring article, but it's ideal for beginners to study because it uses type parameters in short code and uses super and this properly, and it also shows the value of the inheritance function peculiar to object-oriented programming. It is a content that you can experience with.

1. Challenges

In java's ʻArrayList , you can store ʻelement as the ʻindex th element using the method set (int index, T element) . However, if you do this when the number of elements is ʻindex or less, java.lang.IndexOutOfBoundsException will occur. It's sober and troublesome.

2. Solution

I wrote a child class ʻAL of ʻArrayList <T>. Decide in advance "what to fill when the number of elements is insufficient" When the number of elements is actually insufficient, fill it.

The code looks like this:

AL.java


	class AL<T> extends java.util.ArrayList<T>
	{
		private T filler = null;//※

		void fillBy(T filler){this.filler = filler;}

		//If it seems to be set in a place that does not exist, fill it with a filler
		public T set(int index, T element)
		{
			if(filler!=null) while(this.size() <= index) this.add(filler);
			return super.set(index, element);
		}
		//The same is true for get
		public T get(int index)
		{
			if(filler!=null) while(this.size() <= index) this.add(filler);
			return super.get(index);
		}
	}

sample.

Main.java


class Main
{
    public static void main(String...args)
    {
        AL<String> al = new AL<>();
        al.add("indigenous");
        //al.fillBy("I filled.");
        al.set(3, "immigration");
        for(String s : al)
            System.out.println(s);
    }
}

If this is left as it is, an exception will occur and it will not work as before. However, when the commented out fillby method is enabled, no exception is raised and the following is displayed.

indigenous
I filled.
I filled.
immigration

Recommended Posts

[Small story] I tried to make the java ArrayList a little more convenient
[Java] I tried to make a maze by the digging method ♪
I tried to decorate the simple calendar a little
I tried to make a login function in Java
I tried to make a client of RESAS-API in Java
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
[Java small story] Monitor when a value is added to the List
I tried to make a talk application in Java using AI "A3RT"
I tried to make Basic authentication with Java
java I tried to break a simple block
I did Java to make (a == 1 && a == 2 && a == 3) always true
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to break a block with java (1)
A story when I tried to make a video by linking Processing and Resolume
I tried to create a Clova skill in Java
I tried to implement the Euclidean algorithm in Java
I tried to make a program that searches for the target class from the process that is overloaded with Java
A story about running a program that copies files in Java from a bat file to make the work done every day a little more efficient
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
I tried to create a java8 development environment with Chocolatey
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
I want to make a list with kotlin and java!
I just wanted to make a Reactive Property in Java
I want to make a function with kotlin and java!
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to make Java Optional and guard clause coexist
I tried to build the environment little by little using docker
I tried to convert a string to a LocalDate type in Java
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
I tried a little digdag docker.run_options
I tried to interact with Java
I tried to explain the method
I tried the Java framework "Quarkus"
The story I wanted to unzip
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
How to make a Java array
A story that I struggled to challenge a competition professional with Java
I tried to illuminate the Christmas tree in a life game
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
I tried to translate the error message when executing Eclipse (Java)
I tried to make an Android application with MVC now (Java)
I tried to summarize the methods of Java String and StringBuilder
I tried to move the Java compatible FaaS form "Fn Project"
I tried to display the calendar on the Eclipse console using Java.
I tried to make a group function (bulletin board) with Rails
I tried to make a parent class of a value object in Ruby
I tried to make a simple face recognition Android application using OpenCV
How to make a Java calendar Summary
I tried to summarize the methods used
The story of forgetting to close a file in Java and failing
A story I was addicted to when testing the API using MockMVC
I tried to summarize Java lambda expressions
A story that I wanted to write a process equivalent to a while statement with the Stream API of Java8
[iOS] I tried to make a processing application like Instagram with Swift
I tried the new era in Java
I tried to make a Web API that connects to DB with Quarkus
Java beginner tried to make a simple web application using Spring Boot
[Introduction to Java] I tried to summarize the knowledge that I think is essential
How to make a Discord bot (Java)