java学习笔记(六)Arrays

                                   charper 6  Arrays

6.1 introduction to Arrays

        An array behaves like a list of variables with a uniform naming mechanism that can be declared in a single line of simple code.

(1) declaring and creating an Array

                  

说明:

(1)数组就是一类数据类型相同的数据的结合,因此规定数组中的元素的类型必须是相同的。但是可以是任意的类型,自定义也

         可。比如:char[] line = new char[ 80 ];规定该数组的中的元素的内容必须是char类型;

(2)数据如何被访问?

         比如: char[] line = new char[ 80 ];中数组的名字叫line。使用line可以获得整个数组,

         但是如果要获得数组中的单个元素,需要使用line[index] 来获得。比如第一个元素line[0];第二个line[1],最后一个line[79];

         值得注意的是,第一个元素使用的index是0;而不是1;最后一个是79(n-1),而不是n;

         index的范围是有规定的,范围内的数组可以任意使用,否则会报数组越界的错误( out of bounds)

(3)数组的长度。数组在声明的时候,必须给一个具体的长度,否则会报错,比如char[] line = new char[ 80 ];的长度就是80;

         同时可以使用函数line.length 获得数组的长度。

         要注意这里的length是一个instance variable,而不是一个method。我们在计算String的长度的时候,使用的函数length();

        如果声明一个数组后,他的长度可以扩展,但是不可以使用line.length的方式进行改变

(4)数组元素的使用。比如double【】 reading = new double【300】;对于其中的元素就按照一个正常的double 类型的变量来

         使用即可。

(5)如何遍历所有数组中的元素:一般是使用for循环: 5是数组的长度;

        for (index = 0; index < 5; index++)
        {

            System.out.println(score[index] + " differs from max by " + (max – score[index]));

         }

--------------------------------------------------------------------------------------------------------------------------

(2)initializing Arrays

      创建一个数组的时候就可以初始化:

      方法:(1) int[] age = {2, 12, 1}; (使用花括号{},中间使用逗号)

                 (2)int[] age = new int[3];
                          age[0] = 2;
                          age[1] = 12;
                          age[2] = 1;

                 (3)使用for

                      double[] reading = new double[100];
                      int index;
                      for (index = 0; index < reading.length; index++)
                         reading[index] = 42.0;

        如果user不初始化,系统会默认初始化,int---0;double---0.0;booelan--false;

--------------------------------------------------------------------------------------------------------------------------

Pitfall:An Array of Charaters Is Not a String

        比如:char[] a = {'A', ' ', 'B', 'i', 'g', ' ', 'H', 'i', '!'};

        使用该语句是illegal:String s = a; 因为char[]  不是一个String。但是他们之间可以进行转换。因为String自带了构造器:

        String s = new String(a);  

        输出时:System.out.println(a);

        结果:A Big Hi!

 

        同时String也可以有选择的进行转换和输出,比如:

          String s2 = new String(a, 2, 3);

          System.out.println(s2);

          结果:Big

--------------------------------------------------------------------------------------------------------------------------

6.2 Arrays and References

     There are two ways to view an array: as a collection of indexed variables and as a single item whose value is a collection of values of the base type. We will now discuss arrays from the second point of view.

    As we have already seen, every array has an instance variable named  length , which is a good example of viewing an array as an object.

    array types are reference types 数组类型是引用类型

               

--------------------------------------------------------------------------------------------------------------------------

Pitfall:use of = and == with Arrays

    The assignment statement  b = a; copies the memory address from  a to  b so that the array variable  b contains the same memory address as the array variable  a . After the assignment statement,  a and  b are two different names for the same array. Thus, when we change the value of  a[2] , we are also changing the value of  b[2] 

也就是当我们使用b = a 的时候,具相当于给一个地址的数组,两个名字,因此改变其中之一,另一个也会改变。

              

 

如果你希望两个数组中的内容是相同的,只是复制数组的内容,可以使用下面的方法,但是对于数组a,b来说,比如是相同长度。

               

--------------------------------------------------------------------------------------------------------------------------

     A comparison using  == will say they are not equal because  == checks only the contents of the array variables  c and  d , which are memory addresses, and  c and  d contain different memory addresses

     使用== 就是判断两个数组中的内容和memory address是否相同,下图中c,d数组只是内容相同,但是地址不同。

     因此如果只想来比较内容是否相同,不可用“==”

                

 

下列代码检测,内容是否相同,可以自定义一个equalArrays的函数,进行比较。

                             

                             

输出:

                   

--------------------------------------------------------------------------------------------------------------------------

Argument for the Method main

在主函数main 的heading处:public static void main(String[] args)

首先,String[] args,是一个字符串数组,如果你没有给系统一个args的参数,该数组默认是空的,不影响其他程序的执行,但是如果你想使用的,就要通过command line of the operating system,给该数组传递参数。

比如你输入: Do Be Do  下面程序的输出是:Be Do Be

                

具体的执行过程可以参考:https://blog.csdn.net/qq_36098284/article/details/99654903

--------------------------------------------------------------------------------------------------------------------------

Returning an Array 

        注意return语句的写法,还有heading处的写法。

                  

--------------------------------------------------------------------------------------------------------------------------

6.3 Programme with Arrays

for - each  loop

               

--------------------------------------------------------------------------------------------------------------------------

Method with a Variale Number of Parameters

   What we would like is a single method definition for a method named  max that can take any number of  int arguments.

                

      当使用:int highestScore = UtilityClass.max(3, 2, 5, 1); 时

      相当于创建了一个数组:int[] arg = {3, 2, 5, 1};

             

--------------------------------------------------------------------------------------------------------------------------

Enumerated Type

                 

例子:

                           

                           

输出:

                                                        

NB:The values of an enumerated type, such as  WorkDay.THURSDAY , are not  String values.

--------------------------------------------------------------------------------------------------------------------------

6.4 Multidimensional Arrays 多维数组

(1)二维数组

A two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the column(二维数组就相当于是第一个参数表示的是行,第二个是列)

定义&用法:(都与一维数组类似)

                         

比如:

                       

如果想遍历这个二维数组的时候: 使用两个for

                             

--------------------------------------------------------------------------------------------------------------------------

(2)length 对于多维数组的使用

比如:char[][] page = new char[30][100];   length的使用,以及遍历;

             

NB:当使用二维数组的时候,数组名.length == 行数;

--------------------------------------------------------------------------------------------------------------------------

(3)三维数组

如何理解三维数组,请参考:https://blog.csdn.net/qq_36098284/article/details/80621699

--------------------------------------------------------------------------------------------------------------------------

(4) Ragged Arrays

       当然数组也可以是不规则的,这就需要在定义不同的行的时候进行处理

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值