Let's understand the Array (Element) type!

Now that we've learned about the Array (Element) type, we'll output it.

What is Array (Element) type?

*** Array (Element) type is a type that represents an array in a nutshell. *** ***

For example Array (Element) types can be represented using array literals, such as [1,2,3].

qiita.rbvar


1.let a = [a,b,c]
2.let b = [1,2,3]

It is convenient to use the Array (Element) type. You can update, add, join, and delete *** elements for Array (Element) type values.

Let's dig deep one by one!

Element update

qiita.rbvar


               //0 1 2 
1.var numbers = [1,2,3]
2.numbers[1] = [4] //We are updating the first 2
numbers// [1,4,3]

Add element

*** Use the *** append (_ :) *** method to add an element to the end *** The following example adds a "d" of type [String]

qiita.rbvar


1.let strings  = ["a","b","c"]
2.strings append(d)//["a","b","c","d"]

You can also add it to any *** location. To add an element in any *** location, use the *** insert (_: at: 1) ***, method.

In the following example, nsert (_: at: 1) ***, method is used and "b" is added second.

qiita.rbvar


1.let strings  = ["a","c","d"]
2.strings insert("b",at1)//["a","b","c","d"]

Combine elements

You can combine elements of type Array (Element) with the + operator.

qiita.rbvar


1.let strings1 = ["Ah","I","U"]//[String type]
2.let c =["e","O"]//[String type]

let result = strings1+let c //["Ah","I","U","e","O"]

Delete element

To remove an element, delete anywhere *** remove (at :) ***, remove the last element *** removeLast () ***, removeAll () method to remove all elements Three are prepared.

qiita.rbvar


var strings = ["Ah","I","U","e","O"]

strings.remove(at:2)
strings//["Ah","I","e","O"]//"U"Is deleted

strings.removeLast()
strings//["Ah","I","e"]//At the end"O"Is deleted

strings.removeAll()
strings//()

Recommended Posts

Let's understand the Array (Element) type!
Let's understand the Optional (Wrapped) type!
Let's understand the function!
Let's understand the if statement!
Let's understand the for-in statement!
Let's understand the switch statement!
Get the type of an array element to determine if it is an array
Let's understand closures!
Let's attack the vulnerability (1) OS command injection (OGNL type injection)
null specifies the type
Let's make the app better
Let's try the S2Struts tutorial (# 3_180425)
Let's solve the roman numerals
Understand the helper method form_with
Let's try the S2Struts tutorial (# 5_180526)
Let's try the S2Struts tutorial (# 4_180505)
Let's try the S2Struts tutorial (# 1_180423)
About the explanation about functional type
Let's solve the FizzBuzz problem!
Let's try the S2Struts tutorial (# 2_180424)
[Android / Java] Understand the description type in listener settings (button installation)