Arrays Swift 3找到数组中最大双精度的位置

Arrays Swift 3找到数组中最大双精度的位置,arrays,swift3,max,ios10,Arrays,Swift3,Max,Ios10,我的阵列: let array = [45,12,10,90] // The number I need in this case is 3 然后我需要获取另一个数组的值: let otherArray = [6,6,7,4,0] let array = [45,12,10,90] let otherArray = [6,6,7,4,0] if let maxValue = array.max(), let index = array.index(of: maxValue) {

我的阵列:

let array = [45,12,10,90]
// The number I need in this case is 3
然后我需要获取另一个数组的值:

let otherArray = [6,6,7,4,0] 
let array = [45,12,10,90]
let otherArray = [6,6,7,4,0]

if let maxValue = array.max(), let index = array.index(of: maxValue) {
    let desiredValue = otherArray[index]
    print(desiredValue)    // 4
}
我试图解决这样的问题:

let maxPosition = array.max()
let desiredValue = otherArray[maxPosition]
这似乎没有达到预期效果


谢谢你的帮助

问题在于max从数组返回最大值,而不是索引。您需要找到最大值的索引,并将其用于其他数组:

let otherArray = [6,6,7,4,0] 
let array = [45,12,10,90]
let otherArray = [6,6,7,4,0]

if let maxValue = array.max(), let index = array.index(of: maxValue) {
    let desiredValue = otherArray[index]
    print(desiredValue)    // 4
}
另一个选项是在获取最大值时使用集合索引:

if let index = array.indices.max(by: { array[$0] < array[$1] }) {
    let desiredValue = otherArray[index]
    print(desiredValue)    // 4
}
如果let index=array.index.max(by:{array[$0]
问题在于max从数组返回最大值,而不是索引。您需要找到最大值的索引,并将其用于其他数组:

let otherArray = [6,6,7,4,0] 
let array = [45,12,10,90]
let otherArray = [6,6,7,4,0]

if let maxValue = array.max(), let index = array.index(of: maxValue) {
    let desiredValue = otherArray[index]
    print(desiredValue)    // 4
}
另一个选项是在获取最大值时使用集合索引:

if let index = array.indices.max(by: { array[$0] < array[$1] }) {
    let desiredValue = otherArray[index]
    print(desiredValue)    // 4
}
如果let index=array.index.max(by:{array[$0]
以下是另一种方法:

let array = [45,12,10,90]
let otherArray = [6,6,7,4,0]


var maxValueInArray = array[0]
for i in 1..<array.count{
    if array[i] > maxValueInArray{
        maxValueInArray = array[i]
    }
}

if let maxValueIndex = array.index(of: maxValueInArray){
    let desiredValueInOtherArray = otherArray[maxValueIndex]
    print("Maximum value in array is \(maxValueInArray) with index \(maxValueIndex). Value in otherArray under index \(maxValueIndex) is \(desiredValueInOtherArray)")
}
let数组=[45,12,10,90]
让otherArray=[6,6,7,4,0]
var maxValueInArray=数组[0]
因为我在1。。maxValueInArray{
maxValueInArray=数组[i]
}
}
如果让maxValueIndex=array.index(of:maxValueInArray){
让desiredValueInOtherArray=otherArray[maxValueIndex]
打印(“数组中的最大值为\(maxValueInArray)和索引\(maxValueIndex)。索引\(maxValueIndex)下的其他数组中的值为\(desiredValueInOtherArray)”)
}

以下是另一种方法:

let array = [45,12,10,90]
let otherArray = [6,6,7,4,0]


var maxValueInArray = array[0]
for i in 1..<array.count{
    if array[i] > maxValueInArray{
        maxValueInArray = array[i]
    }
}

if let maxValueIndex = array.index(of: maxValueInArray){
    let desiredValueInOtherArray = otherArray[maxValueIndex]
    print("Maximum value in array is \(maxValueInArray) with index \(maxValueIndex). Value in otherArray under index \(maxValueIndex) is \(desiredValueInOtherArray)")
}
let数组=[45,12,10,90]
让otherArray=[6,6,7,4,0]
var maxValueInArray=数组[0]
因为我在1。。maxValueInArray{
maxValueInArray=数组[i]
}
}
如果让maxValueIndex=array.index(of:maxValueInArray){
让desiredValueInOtherArray=otherArray[maxValueIndex]
打印(“数组中的最大值为\(maxValueInArray)和索引\(maxValueIndex)。索引\(maxValueIndex)下的其他数组中的值为\(desiredValueInOtherArray)”)
}

array.max()
将返回
90
,这意味着您正在有效地执行此操作
otherArray[90]
我希望您能明白为什么这会给您一个索引超出范围错误:)提示:您需要90
array的索引。max()
将返回
90
,这意味着您正在有效地执行此操作
otherArray[90]
我希望你能明白为什么这会给你一个索引越界错误:)提示:你需要90的索引,这是可行的,但它在数组中有两次传递。我想有一个版本的
max
以元组的形式返回索引和元素。我现在找不到它,尽管D:谢谢你的解决方案!这是可行的,但它有两次传递通过数组。我想有一个版本的
max
以元组的形式返回索引和元素。虽然我现在找不到它,但D:谢谢你的解决方案!