Array Remove At

From SwiftAPI

Jump to: navigation, search

Contents

C++

For vectors:

#include <vector>
using namespace std;
 
int a0[] = {9, 4, 0, 4, 0};
vector<int> a(a0, a0+5);
int i = 2;
a.erase(a.begin()+i);
// a == [9, 4, 4, 0]

C#

List<int> a = new List<int>() { 9, 4, 0, 4, 0 };
int i = 2;
a.RemoveAt(i);
// a == { 9, 4, 4, 0 }

Java

For Lists:

import java.util.*;
 
List<Integer> a = new ArrayList<Integer>(Arrays.asList(9, 4, 0, 4, 0));
int i = 2;
a.remove(i);
// a == [9, 4, 4, 0]

For arrays this doesn't make sense since arrays are fixed sized.

JavaScript

var a = [9, 4, 0, 4, 0];
int i = 2;
 
a.splice(i, 1);
// a == [9, 4, 4, 0]

Objective-C

developer.apple.com

- (void)removeObjectAtIndex:(NSUInteger)index
NSMutableArray *a = [NSMutableArray arrayWithObjects:@9, @4, @0, @4, @0, nil];
int i = 2;
[a removeObjectAtIndex:i];
// a == [9, 4, 4, 0]

Perl

@a = (9, 4, 0, 4, 0);
$i = 2;
splice(@a, $i, 1);
# @a ==(9, 4, 4, 0)

PHP

$a = array(9, 4, 0, 4, 0);
$i = 2;
array_splice($a, $i, 1);
// $a == array(9, 4, 4, 0)

Python

a = [9, 4, 0, 4, 0]
i = 2
del a[i] # this expression doesn't return anything, or
# a.pop(i) # this expression returns the removed value
# a == [9, 4, 4, 0]

Ruby

ruby-doc.org

array.delete_at(index) -> obj or nil
a = [9, 4, 0, 4, 0]
i = 2
a.delete_at(i)
# a == [9, 4, 4, 0]

Swift

var a = [9, 4, 0, 4, 0]
let i = 2
a.removeAtIndex(i)
// a == [9, 4, 4, 0]
Personal tools