1)用make-array建立數組:python
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)建立二維數組:spa
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訪問數組元素code
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) NIL CL-USER> (aref arr 3) NIL 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> (aref arr 0) A CL-USER> (aref arr 3) O 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 1) B CL-USER> (aref arr 1 0) C CL-USER> (aref arr 1 1) D
4)訪問二維數組element
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) A CL-USER> (aref arr 1 0) C
5)一維數組也是向量it
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)
(未完待續。。。)
class