I want to make a list with kotlin and java!

Introduction ...

This article was written in 2 previous articles,

** 3rd day of article posting every day for 7 days **

It has become

The code to use is pasted below, but see the article two years before for the detailed features of this app!

--java version: https://github.com/sato-na/guruwake_java

--kotlin version: https://github.com/sato-na/guruwake_kotlin

↓ This is the main subject of this article ↓

To work with lists in each language ...

-- table of contents --

  1. How to make an Arraylist

  2. How to add / remove elements

  3. How to get the number of elements

  4. How to reverse / shuffle the contents of the list

  5. How to copy the contents to a different list

How to make an ArrayList

--For java

ArrayList<Element type>Variable name= new ArrayList<>();

Example)

WhoActivity.java


ArrayList<String>memberL = new ArrayList<>();  //20th line

Very different from variable definition

--For kotlin

val/var variable name:ArrayList<Element type> = arrayListOf()

Example)

WhoActivity.kt


var memberL:ArrayList<String> = arrayListOf()  //13th line

Similar to variable definition except how to specify the contents of the list

The method of specifying the element type is very similar for both

How to add / remove elements

--For java

//add to
List name.add(element);
//Delete
List name.remove(Element location);

Example)

WhoActivity.java


//add to
memberL.add(memberET.getText().toString());  //Line 82

ResultActivity.java


//Delete
memberL.remove(0);  //Line 46

--For kotlin

//add to
List name.add(element)
//Delete
List name.remove(element)

Example)

WhoActivity.kt


//add to
memberL.add(member_et.text.toString())  //Line 54

ResultActivity.kt


//Delete
memberL.remove(memberL[0])  //Line 37

The method of adding is the same, but when deleting, the one used to specify the target element is different.

How to get the number of elements

--For java

List name.size();

Example)

resultActivity.java


int memberNum = memberL.size();  //Line 33

--For kotlin

List name.size

Example)

ResultActivity.kt


val memberNum = memberL.size  //24th line

java is very similar to .size () and kotlin is very similar to .size

How to reverse / shuffle the contents of the list

--For java

//Reverse order
Collections.reverse(List name);
//shuffle
Collections.shuffle(List name);

Example)

WhoActivity.java


//Reverse order
ArrayList<String> memberLR = (ArrayList<String>) memberL.clone();  //Line 84
Collections.reverse(memberLR);

resultActivity.java


//shuffle
Collections.shuffle(memberL);  //Line 34

--For kotlin

//Reverse order
List name.reverse()
//shuffle
List name.shuffle(Random())

Example)

WhoActivity.kt


//Reverse order
var memverLR = ArrayList<String>(memberL)  //Line 56
memverLR.reverse()

ResultActivity.kt


//shuffle
memberL.shuffle(Random())  //25th line

The writing style is very different, but both The keywords are "reverse" for reverse order and "shuffle" for shuffle.

How to copy the contents to a different list

--For java

ArrayList<Element type>Variable name= (ArrayList<Element type>)List name to copy.clone();

Example)

WhoActivity.java


ArrayList<String> memberLR = (ArrayList<String>) memberL.clone();  //Line 84

--For kotlin

val/var variable name= ArrayList<Element type>(List name to copy)

Example)

WhoActivity.kt


var memverLR = ArrayList<String>(memberL)  //Line 56

java uses .clone (), kotlin is a little different from the list definition method

Finally…

This time I defined the list in java and kotlin. I think there are still many features, so I hope I can write an article when it comes out in other code.

In addition, the schedule from today has been slightly changed from the schedule described on the first day. I'll change the schedule of that article, so it doesn't really matter. I will write it here for my own memo.

I will post an article tomorrow, so please keep an eye on me.

Recommended Posts

I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to transition screens with kotlin and java!
I want to implement various functions with kotlin and java!
I want to return to the previous screen with kotlin and java!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to make a button with a line break with link_to [Note]
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I tried to make Basic authentication with Java
I did Java to make (a == 1 && a == 2 && a == 3) always true
I want to use java8 forEach with index
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to break a block with java (1)
I want to display images with REST Controller of Java and Spring!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I want to ForEach an array with a Lambda expression in Java
How to make an app with a plugin mechanism [C # and Java]
I want to monitor a specific file with WatchService
I tried to make a login function in Java
I want to get along with Map [Java beginner]
I used to make nc (netcat) with JAVA normally
[Kotlin] I wanted to generate a png with a large capacity per area [Java]
Rails6 I want to make an array of values with a check box
[Android] I tried to make a material list screen with ListView + Bottom Sheet
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I tried to create a shopping site administrator function / screen with Java and Spring
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
How to make a Java container
I want to make a specific model of ActiveRecord ReadOnly
I want to call a method and count the number
I just wanted to make a Reactive Property in Java
I tried to interact with Java
I tried to make Java Optional and guard clause coexist
Make a list map with LazyMap
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I tried to make a client of RESAS-API in Java
How to make a Java array
[Java] I want to test standard input & standard output with JUnit
I want to implement it additionally while using kotlin on a site running Java
I want to write a loop that references an index with Java 8's Stream API
I want to get a list of the contents of a zip file and its uncompressed size
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
I want to return a type different from the input element with Java8 StreamAPI reduce ()
A story that I struggled to challenge a competition professional with Java
I tried to make an Android application with MVC now (Java)
[Java] I tried to make a maze by the digging method ♪
[Java] I want to perform distinct with the key in the object
I want to perform asynchronous processing and periodic execution with Rail !!!
I want to extract between character strings with a regular expression
I tried to make a group function (bulletin board) with Rails
I want to develop a web application!
How to make a Java calendar Summary
I want to write a nice build.gradle
Make a family todo list with Sinatra
I want to make an ios.android app
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
How to make a Discord bot (Java)
Make a family todo list with Sinatra