如何在swift中复制数组的结尾?

时间:2016-05-24 13:17:13

标签: swift

必须有一些非常优雅的方法使用Swift从某个索引开始复制数组的结尾,但我找不到它,所以我以此结束:

func getEndOfArray<T>( arr : [T], fromIndex : Int? = 0) -> [T] {
    var i=0;
    var newArray : [T] = [T]()
    for item in arr {
        if i >= fromIndex {
            newArray.append(item)
        }
        i = i + 1;
    }
    return newArray // returns copy of the array starting from index fromIndex
}

在没有额外功能的情况下,有更好的方法吗?

6 个答案:

答案 0 :(得分:9)

您可以使用后缀:

let array = [1,2,3,4,5,6]
let lastTwo = array.suffix(2) // [5, 6]

正如评论中所提到的:这为您提供了一个ArraySlice对象,这对大多数情况来说已经足够了。如果你真的需要一个Array对象,你必须投射它:

let lastTwoArray = Array(lastTwo)

答案 1 :(得分:8)

另一个......

let array = [1, 2, 3, 4, 5]
let fromIndex = 2
let endOfArray = array.dropFirst(fromIndex)
print(endOfArray) // [3, 4, 5]

这给出ArraySlice,对大多数人来说应该足够好 目的。如果您需要真实Array,请使用

let endOfArray = Array(array.dropFirst(fromIndex))

如果起始索引大于(或等于)元素数,则会创建一个空数组/切片。

答案 2 :(得分:3)

这是一种可能的解决方案,可能还有一些

Public Function CompareTableSQL(Table1 as String, Table2 as String, Optional Table2Path as String = vbNullString) as String
  Dim Table1Field as string, Table2Field as string
  Dim tdf as DAO.TableDef, db as DAO.Database, f as DAO.Field2
  Set db = CurrentDB
  Set tdf = db.TableDefs(Table1)
  For each f in tdf.Fields
    Table1Field = Table1Field & " & ""+"" & CStr(nz([" & Table1 & "].[" & f.name & "],""NULL""))"
    Table2Field = Table2Field & " & ""+"" & CStr(nz([" & Table2 & "].[" & f.name & "],""NULL""))"
  Next
  ' Extracting the primary key and joining on it is relatively easy, _
    but that's not really the point of this, so I'm leaving it out.
  SQL = "SELECT [" & Table1 & "].* FROM [" & Table1 & "] " & vbcrlf & _
        "LEFT JOIN [" & Table2 & "] ON [" & Table1 & "].ID = [" & Table2 & "].ID " & vbcrlf
        "WHERE " & vbcrlf & _
        Table1Field & "<>" & Table2Field & ";"
End Function

或使用动态索引和范围检查

let array = [1, 2, 3, 4, 5]
let endOfArray = Array(array[2..<array.endIndex]) // [3, 4, 5]

由于let array = [1, 2, 3, 4, 5] let index = 2 let endOfArray : [Int] if index < array.count { endOfArray = Array(array[index..<array.endIndex]) // [3, 4, 5] } else { endOfArray = array } 的范围订阅返回Array

,因此需要重新初始化数组

答案 3 :(得分:1)

您可以在Swift 4中使用新方法removeFirst(_:)

function sendTheAJAX(successFunc, context){
  var xhr = new XMLHttpRequest, c = context || this;
  xhr.open('GET', 'http://www.classiperlo.altervista.org/File%20comuni/ora_ajax.php');
  if(xhr.readyState === 4 && xhr.status === 200){
    successFunc.call(c, xhr.responseText);
  }
  xhr.send();
  return xhr;
}
function yup(){
  sendTheAJAX(function(respTxt){
    document.getElementById('ajax-content').innerHTML = respTxt;
  });
}
yup(); // remove if you don't want call here
document.body.onclick = yup;

答案 4 :(得分:1)

您现在可以下标一个开放式范围。这将包括从索引5开始的元素。

Swift 4.2

array[5...]

答案 5 :(得分:0)

您可以使用filter

func getEndOfArray( arr : [Int], fromIndex : Int? = 0) -> [Int] {
    let filteredArray = arr.filter({$0 <= fromIndex})

    return filteredArray
}
为了这个例子,

冒昧地改为[Int]