如何从swift 3中给出的其他数组中删除所有索引值[重复]

时间:2021-12-14 04:14:27

This question already has an answer here:

这个问题在这里已有答案:

suppose i have a string array in swift 3 with values

假设我在swift 3中有一个带有值的字符串数组

var Names:[String] = ["john","Obama","Trump"]

in my other number array i have values

在我的其他数字数组中,我有价值观

var index int[]  = [0,2]

i want to delete all the values from Names array except index 0 and 2 thats listed in my second numeric array list so my result sould like ["john","Trump"]

我想删除Names数组中的所有值,除了我的第二个数字数组列表中列出的索引0和2,所以我的结果应该像[“john”,“特朗普”]

i am working in swift 3

我在swift 3工作

2 个解决方案

#1


2  

var names = [String]()
var indexes = [Int]()

Create newNames

var newNames = [String]()

Populate newNames

for idx in indexes {
   newNames.append(names[idx])
}

#2


0  

to remove all elements exepts the elements contained in the list indexs:

删除列表索引中包含的元素的所有元素:

var names = [String]()
names.append("john")
names.append("Obama")
names.append("Trump")

var indexs = [Int]()
indexs.append(0)
indexs.append(2)

var names_res = [String]()


for index in indexs {
       names_res.append(names[index])
}

#1


2  

var names = [String]()
var indexes = [Int]()

Create newNames

var newNames = [String]()

Populate newNames

for idx in indexes {
   newNames.append(names[idx])
}

#2


0  

to remove all elements exepts the elements contained in the list indexs:

删除列表索引中包含的元素的所有元素:

var names = [String]()
names.append("john")
names.append("Obama")
names.append("Trump")

var indexs = [Int]()
indexs.append(0)
indexs.append(2)

var names_res = [String]()


for index in indexs {
       names_res.append(names[index])
}