Swift and Java comparison of collection types

Source: Internet
Author: User

1. Array declarations

The arrays in Swift:swift are type-safe, and the data values must be explicit before being stored into an array, and the types they contain must be clear, and the code is as follows:

Let array:array<string>  = ["Eggs""Milk"  = ["Eggs""Milk"= ["Eggs  ""Milk"]

The Java Array declaration code is as follows:

String[] Array = {"Eggs","Milk"};

2. Accessing and modifying arrays

Java mainly uses sets of array encapsulation, provide APIs externally, in Swift, the array has a lot of APIs for developers to call, and the Java array does not have many APIs, commonly used is the length of this property. Here are some examples of Swift's APIs about arrays.

var array: [String] = ["Eggs","Milk"] Array.append ("Flour") Array+= ["Chocolate Spread","Cheese","Butter"]        ifarray.isempty {print ("array is empty")                    }Else{print ("array is not empty") Print ("The first item is \ (Array[0])")        }

This code is primarily about an array of non-null, value, and add Array item APIs.

2.1 Modifying array elements

var array: [String] = ["Eggs","Milk","Chocolate Spread","Cheese","Butter"] array[0] ="Big Eggs"//Modify the first element of an arrayarray[1...3] = ["Big Milk","Big Chocolate Spread","Big Cheese"]       //Modify the second to fourth element of an array         forItemincharray{Print ("The item is \ (item)")     }

2.2 Array insert element and delete element

var array: [String] = ["Eggs","Milk","Chocolate Spread","Cheese","Butter"] Array.insert ("Test Insert", Atindex:0) Array.removeatindex (4)         forItemincharray{Print ("\ (item)")        }

2.3 Traversal of an array

For in mode, the code is as follows:

 var array: [String] = [ eggs   ", "  milk  , "  chocolate Spread   ", "  cheese  , "  butter   "  for  item in   array{print (item)}  

If we need values and index values for each data item at the same time, you can use global enumerate functions for array traversal. enumeratesequence Returns a tuple that consists of index values and data values for each data item. We can break this tuple into temporary constants or variables to traverse:

var array: [String] = ["Eggs","Milk","Chocolate Spread","Cheese","Butter"]         for(Index, value)inchenumeratesequence (array) {print ("Item \ (index + 1): \ (value)")        }

2.4 Array Initialization

The array initialization API is as follows:

3 " Item "  = [Int] ()// Declare an empty array

Summary: Here are some notes on the operation of the Swift array, unlike Java, where Swift provides a large number of APIs for array operations.

3. The Swift dictionary and the Java Map Collection comparison

A dictionary and a map collection are like a simple statement that defines a collection of dictionaries that have one or more key-value pairs, and a key-value pair is a key value combination of one and the other.

The Swift dictionary defines the code as follows:

var airports: [String:string] = ["Tyo":"Tokyo","DUB":"Dublin"]var airports1= ["Tyo":"Tokyo","DUB":"Dublin"]//define a dictionary in Swift

3.1 Reading and modifying dictionaries

Swift modifies the dictionary API as follows:

airports[values for"LHR""London Heathrow"//  "LHR" was changed to "London HeathrowAirports.updatevalue ("London Heathrow""LHR  ")//  " LHR "corresponding value was changed to" London Heathrow

The dictionary delete operation, the code is as follows:

airports["APL"= nil//  APL is now removed Airports.removevalueforkey ("LHR") // APL is now removed .

3.2 Dictionary Traversal

We can use for-in loops to iterate through key-value pairs in a dictionary. The data items in each dictionary are returned in the (key, value) form of tuples, and we can use temporary constants or variables to decompose the tuples, with the following code:

var airports: [String:string] = ["Tyo":"Tokyo","DUB":"Dublin"]         for(Airportcode, Airportname)inchairports{Print ("The Airportcode is \ (Airportcode) and the airportname are \ (airportname)")} print ("Anthor Loop in dictionary")         forAirportcodeinchairports.keys{Print ("Airportcode = \ (airportcode)")        }

If we just need to use a dictionary key set or a value set as a parameter to an accepted Array instance API, you can directly use the keys or values property to construct a new array directly, with the following code:

Let airportcodes = Array (airports.keys)//  airportcodes is ["Tyo", "LHR"]

Swift and Java comparison of collection types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.