함수 array()

(New array에서 넘어옴)

1 ActionScript[ | ]

var myArray:Array = new Array("a", "b", "c");
trace(myArray);
// Output: a,b,c.

2 Bash[ | ]

ARR=("John Smith" "Jane Doe" "Mike Barnes")
echo ${ARR[0]}
# John Smith
echo ${ARR[1]}
# Jane Doe
ARR=(
"John Smith"
"Jane Doe"
"Mike Barnes"
)
ARR=()
ARR+=("John Smith")
ARR+=("Jane Doe")
ARR+=("Mike Barnes")
NAMES[21]="John Smith"
NAMES[99]="Jane Doe"
echo ${NAMES[21]}
# John Smith
echo ${NAMES[99]}
# Jane Doe

3 C++[ | ]

#include <iostream>
#include <vector>
using namespace std;
int main() {
	vector<int> nums = {1,2,3,4};
	for(int i=0; i<nums.size(); i++) cout << nums[i] << " ";
	// 1 2 3 4 
}

4 C#[ | ]

int[] numbers = new int[5];
string[] arr = { "a", "b", "c" };

5 Go[ | ]

package main
import "fmt"
func main() {
	x := [5]int{ 98, 93, 77, 82, 83 }
	fmt.Println( x )
	// [98 93 77 82 83]
}

6 Java[ | ]

array
Object[] arr = {42, "Banana", "yellow", 1129};
System.out.println( Arrays.toString(arr) );
// [42, Banana, yellow, 1129]
int[] arr = new int[3];
System.out.println( Arrays.toString(arr) );
// [0, 0, 0]
int[] arr = new int[] {1, 3, 5};
System.out.println( Arrays.toString(arr) );
// [1, 3, 5]
int[] arr = {1, 3, 5};
System.out.println( Arrays.toString(arr) );
// [1, 3, 5]
List
List<Object> a = Arrays.asList(42, "Banana", "yellow", 1129);
System.out.println( a.toString() );
// [42, Banana, yellow, 1129]
List<Integer> a = Arrays.asList(1, 2, 3);
System.out.println( a.toString() );
// [1, 2, 3]
List<String> a = Arrays.asList("a", "b", "c");
System.out.println( a.toString() );
// [a, b, c]
List<String> mylist = new ArrayList<String>();
mylist.add("Mike Barnes");
mylist.add("Kevin Patterson");
System.out.println( a.toString() );
// [Mike Barnes, Kevin Patterson]

7 JavaScript[ | ]

var fruits = ["Apple", "Banana", "Orange", "Mango"];
var numbers = [5, 6, 3, 2, 11, 9];
var mixed = [42, "Banana", "yellow", 1129];
var foo = new Array( 3.14, 777, 'Hello', -0.5, 'World');
var bar = [];
bar[0] = 3.14;
bar[1] = -0.5;
bar[2] = 'World';
var bar = new Array();
bar[0] = 3.14;
bar[1] = -0.5;
bar[2] = 'World';

8 Objective-C[ | ]

NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"a", @"b", @"c", nil];
NSMutableArray *arr = [NSMutableArray arrayWithObjects:nil];
[arr addObject:@"a"];
[arr addObject:@"b"];
[arr addObject:@"c"];

9 PHP[ | ]

$nums = [1,2,3];
$nums = array(1,2,3);
$nums = array();
$nums[] = 1;
$nums[] = 2;
$nums[] = 3;

10 Python[ | ]

nums = [1,2,3,4]
print(nums)
# [1, 2, 3, 4]
print(nums[0])
# 1
nums = []
nums.append(1)
nums.append(2)
print(nums)
# [1, 2]

11 Ruby[ | ]

my_array = [1,2,3]
print my_array
# [1, 2, 3]
my_array = [1,2,3]
puts my_array
# 1
# 2
# 3

12 Perl[ | ]

my @arr = (1, 2, 3);
foreach my $item (@arr) {
	print "$item\n";
}
# 1
# 2
# 3
print $arr[1] . "\n";
# 2

13 POSIX Shell[ | ]

ARR="1 2 3 4 5"
for item in $ARR
do
	echo $item
done
# 1
# 2
# 3
# 4
# 5

14 Windows Batch[ | ]

@echo off
setlocal enabledelayedexpansion

set arr[0]=1
set arr[1]=2
set arr[2]=3
set arr[3]=4

for /l %%n in (0, 1, 3) do (
	echo !arr[%%n]!
)
@echo off
set ARR=(1 2 3 4)
for %%i in %ARR% do echo %%i

15 같이 보기[ | ]

16 참고[ | ]