Array Clear

From SwiftAPI

Jump to: navigation, search

Remove all elements from an array

Contents

C++

For vectors:

#include <vector>
using namespace std;
 
int a0[] = {1, 2, 3, 4};
vector<int> a(a0, a0+4);
a.clear();
// a == []

C#

List<int> a = new List<int>() { 1, 2, 3, 4 };
a.Clear();
// a == { }

Java

For Lists:

import java.util.List;
import java.util.Arrays;
 
List<Integer> a = Arrays.asList(1, 2, 3, 4);
a.clear();
// a == []

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

JavaScript

var a = [1, 2, 3, 4];
a.length = 0;
// a == []

Objective-C

developer.apple.com

- (void)removeAllObjects
NSMutableArray *a = [NSMutableArray arrayWithObjects:@1, @2, @3, @4, nil];
[a removeAllObjects];
// a == []

Perl

@a = (1, 2, 3, 4);
@a = ();
# @a == ()

or

@a = (1, 2, 3, 4);
splice(@a);
# @a == ()

or

@a = (1, 2, 3, 4);
$#a = -1;
# @a == ()

PHP

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

Python

a = [1, 2, 3, 4]
del a[:]
# a == []

Ruby

a = [1, 2, 3, 4]
a.clear
# a == []

Swift

var a = [1, 2, 3, 4]
a.removeAll()
// a == []
Personal tools