Javase basic Array Object-oriented programming understanding some key words ' explanation and usage

Source: Internet
Author: User
Tags array length

One-dimensional arrays

Arrays: You can store multiple elements and multiple elements are the same type of container

Definition of the array:

   数据类型[] 数组名;               例如:int[]a;  意义:定义了一个int类型的数组变量a   数据类型 数组名[];               例如:int a[];  意义: 定义了一个int类型变量a数组

Note: Although the two definitions are read and written differently, the effect of the expression is to define an array, the first method is recommended;

Initialization of the array:

             1)动态初始化:  给数组指定长度,数组中的元素值由系统默认给定                          数据类型[] 数组名 = new 数据类型[数组长度] ;             2)静态初始化:  给定数组中元素的值,数组的长度由系统给定;                                 数据类型[] 数组名称= {元素1,元素2,元素3..} ;

Initialization error: Static and dynamic combination defines the array either dynamically initialized, specifying the length, or statically initializing the specified array element!

How to get the elements in an array

     获取数组中的元素:通过数组名称获取

Format: array name [index value], index value of array starting from 0, maximum index = array Length-1

Exceptions that often occur in arrays:

             1>ArrayIndexOutOfBoundsException:数组角标越界异常                               出现的原因:访问了数组中不存在的索引值;                               解决方案:观察数组中到底有索引值到多少;  数组长度-1            2>NullPointerException:空指针异常 (在实际开发中该异常是最多的)                               出现的原因:对象为null,而开发者还要去使用对象,就会出现问题                               解决方案:给某个对象进行非空判断,防止程序程序空指针异常

Array of applications:

     1>数组的遍历:               publicstatic void printArray2(int[] arr) {               //左中括号               System.out.print("[");               for(intx = 0 ; x < arr.length ; x ++) {                        //判断当前x是否是最后一个索引                        if(x==arr.length-1){//如果是最后一个索引的对应的元素                                 System.out.println(arr[x]+"]");                        }else{                                 System.out.print(arr[x]+",");                        }               }     }     2>数组的最值问题:               publicstatic int max(int[] arr) {               //定义参照物               intmax = arr[0] ;               //遍历其他元素               for(intx = 1 ; x < arr.length ; x ++) {               //获取到每一个元素,分别和参照物进行比较,如果大了,就作为最大值进行继续比较                        if(arr[x]> max) {                                 max= arr[x] ;                        }               }               returnmax ;     }       

3> Array in reverse order:

     /*

* Defines an array, static initialization

    • Swaps the elements of the 0 index in the array with the corresponding elements of the arr.length-1 index

    • Swaps the elements corresponding to the 1 index and the elements of the Arr.length-1-1 index

    • ....

    • Just keep the length of the array arr.length

*/

     publicstatic void revers2(int[] arr) {               for(intstart = 0,end = arr.length-1 ; start<=end ;start++,end --) {                        //temp中间变量                        inttemp = arr[start] ;                        arr[start]= arr[end] ;                        arr[end]= temp ;               }     }

4> Element Lookup:

Element lookup methods in arrays (basic lookup in arrays)

    • Defines an array, statically initialized, to find the index of the first occurrence of an element in the array

    • Analysis:

    • 1) gives the array

    • 2) Write a method to find the index of an element in an array

       publicstatic int getIndex(int[] arr,int value) {           //如果没有找到这个元素,假设找不到           intindex = -1 ;           //遍历数组           for(intx = 0 ;  x < arr.length ; x ++) {                    //获取到每一个元素,如果找打,修改索引值                    if(arr[x]== value) {                             //修改                             index= x ;                             break;                    }           }           returnindex ; }

To improve program execution efficiency, Java allocates memory to 5 parts:

                                                    Java基础之类与对象

Before introducing object oriented, we should know the process-oriented thought

Process oriented:

     假设有一个需求:求数组中的最大值;     定义一个数组,静态初始化---->定义一个参照物---->遍历....     给一个需求,对需求分析,然后一步一步进行操作,最终得到某一个结果,整个过程都是自己在完成....

An overview of object-oriented thinking:

               思想的特征:                        1)面向对象更符合我们的生活中的行为思想习惯                        2)面向对象将我们从执行者变成了指挥者,指挥对象做事情                        3)面向对象简单性体现在这里:让我们事情从复杂性--->简单化

Three main features of object-oriented:

                        封装                        继承                        多态

Example:

     炒菜:                                 面向过程:买菜--->摘菜--->洗菜--->切菜--->炒菜--->出锅                                 面向对象:找一个对象..--->出锅...

Object-oriented implementation in code:

     将事物----->看成类

Attribute---> As member variable of this class

     将事物对应的行为----->看成这个类的成员方法

A standard class includes:

            成员变量:姓名,年龄,性别            成员方法:set/getXXX()                               学习,睡觉...                       构造方法:                     无参构造:                     有参构造

* Member variables

Interview questions:

The difference between a member variable and a local variable

            1)在类中位置不同                     成员变量:类中,方法外                     局部变量:方法声明上,或者再方法定义中            2)在内存中的位置不同:                     成员变量:堆内存                     局部变量:栈内存            3)生命周期不同:                     成员变量:成员变量是随着类的加载而加载,随着类的消失而消失                     局部变量:随着方法的调用而存在,随着方法调用完毕而消失            4)初始化值不同                     成员变量:初始化由系统默认初始化,                                 对于成员变量,可以不给初始化值,由系统给,然后显示初始化;                     局部变量:定义一个局部变量,必须进行初始化,如果不初始化,无法使用(在使用之前进行初始化)

* Member method

Classification of member methods:

                               1)按照返回值划分                                        void的形式:没有具体的返回值                                        非void形式:有具体返回值                               2)按照参数进行划分                                        空参                                        有参

* Construction Method

Construction method Effect:

             就是给对象进行进行初始化

Characteristics of the construction method:

               1)方法名和类名相同            2)构造方法,连void都没有

Considerations for construction methods:

            1)之前没有写无参构造,系统会默认提供无参构造            2)如果我们无参构造或者有参构造,系统不会在提供无参构造;定义一个类的时候,永远给出无参构造;     构造方法是可以重载的

The process of creating an object experience:

Anonymous objects: Objects that are not named when the object is created, but still open up space in the heap memory

     new对象

Anonymous object in the actual development, use only once, do not use multiple times (may cause a memory overflow phenomenon);

Problems with formal parameters:

                       如果是基本数据类型,形式参的改变对实际参数没有影响                     研究引用类型:形式参数的改变会直接影响实际参数

In Java, create an object: The new object

Class Name Object name = new class name ();

Several key words commonly used in Java basics

Usage of private:

                     1)被private修饰的只能在本类访问                     2)可以通过公共的访问public去间接访问

When the current member variable and local variable name are consistent, follow the nearest principle

Private: Embodiment is also a package of ideas

Encapsulation: The standard class of writing, the member variables are all privatized, private modified member variables can only be accessed in this class, you can access the member variables through public access methods

This keyword:

This: Represents the current class object, or (a reference to the current class object), resolves a locally hidden member variable

For example:

     publicvoid setBrand(String brand) {

Brand= brand;

               this.brand= brand;     }

About the Static keyword:

            1)静态随着类的加载而加载            2)static优先于对象存在                     回想:main  public static void main(..){...}             3)static共享数据,可以被多个对象进行共享                     举例:        饮水机(staticd的:共享的)                                        水杯(不能共享的...)            4)如果数据被静态修饰的,它可以被类名直接调用                                        被静态修饰的方法:        类名.方法名()                                        被静态修饰的变量:        类名.变量名;

Static usage:

            一个类中可有静态变量,也可以有非静态            可以有静态成员方法,也可以非静态的成员方法            静态的方法只能访问静态变量,或者是静态的方法            非静态的方法,既可以访问静态变量也可以非静态的变量...               Java基础之说明书的制作

Tool classes for array manipulation

 如何制作一个文档说明书                    1)创建一个数组,静态初始化                    2)创建一个工具类,ArrayTool,提供一些静态功能                                       遍历,最值,查找                    3)ArrayTool,每类,方法加上文档注释                    4)打开dos控制台:                             找到当前路径:                                                javadoc-d(目录名) -author -version ArrayTool.java

Javase basic Array Object-oriented programming understanding some key words ' explanation and usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.