1)用make-array创建数组:
CL-USER> (make-array 5 :initial-element nil)#(NIL NIL NIL NIL NIL)CL-USER> (make-array 5 :initial-contents '(a e i o u))#(A E I O U)CL-USER> (setf arr (make-array 5 :initial-contents '(a e i o u)))#(A E I O U)CL-USER> arr#(A E I O U)
也可以简单一点:
CL-USER> (setf a '#(nil nil nil nil nil))#(NIL NIL NIL NIL NIL)
2)创建二维数组:
CL-USER> (setf arr (make-array '(2 3) :initial-element nil))#2A((NIL NIL NIL) (NIL NIL NIL))CL-USER> (make-array (list 2 2))#2A((0 0) (0 0))CL-USER> (make-array (list 2 2) :initial-contents '((1 2) (3 4)))#2A((1 2) (3 4))CL-USER> (make-array '(2 2) :initial-contents '((a b) (c d)))#2A((A B) (C D))CL-USER> (setf arr (make-array '(2 3) :initial-element nil))#2A((NIL NIL NIL) (NIL NIL NIL))CL-USER> arr#2A((NIL NIL NIL) (NIL NIL NIL))
3)用aref访问数组元素
CL-USER> (setf arr (make-array '(2 3) :initial-element nil))#2A((NIL NIL NIL) (NIL NIL NIL))CL-USER> arr#2A((NIL NIL NIL) (NIL NIL NIL))CL-USER> (make-array 5 :initial-element nil)#(NIL NIL NIL NIL NIL)CL-USER> (setf arr (make-array 5 :initial-element nil))#(NIL NIL NIL NIL NIL)CL-USER> (aref arr 0)NILCL-USER> (aref arr 3)NILCL-USER> (setf arr (make-array 5 :initial-contents '(a e i o u)))#(A E I O U)CL-USER> arr#(A E I O U)CL-USER> (aref arr 0)ACL-USER> (aref arr 3)OCL-USER> (setf arr (make-array '(2 2) :initial-contents '((a b) (c d))))#2A((A B) (C D))CL-USER> arr#2A((A B) (C D))CL-USER> (aref arr 0 1)BCL-USER> (aref arr 1 0)CCL-USER> (aref arr 1 1)D
4)访问二维数组
CL-USER> (setf arr (make-array '(2 2) :initial-contents '((a b) (c d))))#2A((A B) (C D))CL-USER> arr#2A((A B) (C D))CL-USER> (aref arr 0 0)ACL-USER> (aref arr 1 0)C
5)一维数组也是向量
CL-USER> (vector "a" 'b 5)#("a" B 5)CL-USER> (setf arr (vector "a" 'b 5))#("a" B 5)CL-USER> arr#("a" B 5)
(未完待续。。。)