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 ↓
-- table of contents --
How to make an Arraylist
How to add / remove elements
How to get the number of elements
How to reverse / shuffle the contents of the list
How to copy the contents to a different list
--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
--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
.GetText ()
will be explained at a later date.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
.Text
will be explained at a later date.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.
--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
--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.
--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
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