zoukankan      html  css  js  c++  java
  • 【Linux】数组与关联数组

    数组

    数组的定义:

    variable=(arg1 arg2 arg3 …)

    中间用空格分开,数组的下标从0开始

    1.获取下标为n的元素

    variable[n]

    不存在数组溢出的情况,如果下标n>=数组长度,那么为空,不会报错。

    [root@localhost test]# var=(1 2 3)
    [root@localhost test]# echo ${var[0]}
    1

    2.获取数组长度

    ${#var[@]}或者${#var[*]}

    [root@localhost test]# echo ${#var[@]}
    3
    [root@localhost test]# echo ${#var[*]}
    3

    3.循环遍历数组

    语法:

    for i in ${var[@]};do

    #do something…

    done

    也可以将上述@换成*

    关联数组

    在关联数组中,我们可以用任意的文本作为数组索引

    [root@localhost test]# declare -A Arr
    [root@localhost test]# Arr=([pos1]=zhangsan [pos2]=Lisi
    [root@localhost test]# echo ${Arr[pos1]}
    zhangsan

    也可以使用独立的索引对数组赋值

    [root@localhost test]# Arr[pos3]=Wangwu

    列出数组所以的索引值(关联数组与普通数组都通用)

    [root@localhost test]# echo ${!Arr[@]}
    pos2 pos3 pos1
    [root@localhost test]# echo ${!Arr[*]}
    pos2 pos3 pos1

  • 相关阅读:
    npm 安装Vue环境时报错
    WinSCP与SecureCRT
    LeetCode100---Same Tree
    LeetCode404---Sum of Left Leaves
    LeetCode283---Move Zeroes
    LeetCode344---Reverse String
    Java多线程一
    Java知识点总结
    Java泛型
    深入浅出设计模式学习笔记四:单例模式
  • 原文地址:https://www.cnblogs.com/OliverQin/p/9750103.html
Copyright © 2011-2022 走看看