[Road to Final Review] Java (4) A

According to the above, we wrote about arrays in this chapter. After the arrays are finished, our foundation is basically solid, and then we start to update object-oriented. Object-oriented is the mid-term content, and the latter will pass faster. My plan , the content of Java will be updated almost at the end of June, and then a series of content such as database, data structure and algorithm will be updated, so stay tuned!

Article directory

an array

The characteristics of the two groups

Classification of three arrays

The use of four one-dimensional arrays

Initialization of five one-dimensional arrays

The use of six one-dimensional arrays

Traversal of seven one-dimensional arrays

Default value for eight array elements

Nine summary


foreword

In the last chapter, we finished talking about the cycle system, let us open a new series of problems to explore.


提示:以下是本篇文章正文内容,下面案例可供参考

an array

Array: It is a collection of multiple data of the same type arranged in a certain order, and is named with a 1 name, and these data are managed in the same way by numbering

The concept of an array: the subscript of the array name (also called the index) the length of the element array

In our impression, we can imagine our arrays on the menus in restaurants and on Meituan when we order takeaways. What are the characteristics of arrays? Let's look down.

The characteristics of the two groups

1. The array itself is a reference data type, and the elements in the number can be of any data type, including data types and reference data types

2. Creating an array object will open up a whole continuous space in the memory, and the space occupied depends on the length of the array and the type of elements in the array

3, and the array elements are closely arranged and ordered in memory.

4. Once the array is initialized, its length is determined, and once the array is determined, it cannot be modified

5. The array name refers to the first address of this continuous space

Classification of three arrays

1. Classified by element type:

  • array of primitive data type elements
  • an array of reference data type elements

2, according to the dimension

  • one-dimensional array
  • Two-dimensional array

The use of four one-dimensional arrays

The format of a one-dimensional array:

Data type of element [ ] One-dimensional array name

int[] arr;
int arr1[];
double[] arr2;
String[] arr3; //引用类型变量数组

Array declaration:

  • Dimensions of arrays: in Java. [ ] means one-dimensional array [ ] [ ] means two-dimensional array
  • The element type of the array: the data of the data type that the created array container can store can be a Java data type. For example int String Student
  • Array name: It is an identifier representing an array, and the array name is also a variable name. The array name ten variables refer to the data type because it represents a set of data

Initialization of five one-dimensional arrays

There are two types of initialization for one-dimensional arrays, one is static initialization and the other is dynamic initialization. Let's talk about the difference between these two initializations!

Static initialization:

  • If the initialization of the array variable and the assignment of the array elements are performed at the same time, then the static initialization of the
  • Static initialization, the essence is to initialize the array with static data, at this time the length of the array is determined by the number of static data

The format of a one-dimensional array:

Data type [] array name = new data type [] {1,2,3,....}

or

data type [ ] array name;

Array name = new data type [ ]{1,2,3,....} 

Where new is a keyword, the keyword used to create an array. Because the array itself is a reference data type, use new to create an array entity

int[] arr = new int[]{1,2,3,4,5};
//或者下面这种
int[] arr;
arr = new int[]{1,2,3,4,5};

dynamic initialization

The initialization of the array variable is performed separately from the assignment operation of the array, which is dynamic initialization.

In dynamic initialization, only the number of elements (that is, the length of the array) is determined, and the array elements are only default values ​​at this time, and the expected values ​​have not been assigned. The actual expected data needs to be assigned one by one.

Format:

The data type of the elements stored in the array [ ] array name = new The data type of the elements stored in the array [length];

or

The data type stored in the array [ ] the name of the array;

Array name = new data type stored in the array [length];

The length of the length array here indicates the maximum number of elements that can be stored in the array container

The array is limited in length and has a certain length. For example, if we have a 500ml bottle, then it is at most 500ml, which is fixed.

The use of six one-dimensional arrays


The length of the array:
the total number of elements in the array, that is, the length of the array

Each array has an attribute length indicating its length, for example: arrlength indicates the length of the array arr (that is, the number of elements)

Every array has a length, and once initialized, its length is fixed and immutable.

How to represent an element in an array?

Each element stored in the array will automatically have a number, starting from 0, this automatic number is called the array index (index) or subscript, and the elements in the array can be accessed through the index/subscript of the array.

Array name [index/subscript]

The subscript range of the array?

In Java, the subscript of the array starts from [0], and the subscript range is [0, the length of the array - 1], that is, [0, the array name. Length - 1]

Array element subscripts can be integer constants or integer expressions. a[3], b[i].

Traversal of seven one-dimensional arrays

Obtaining each element in the array separately is traversal. The for loop and the traversal of the array are a perfect match.

public class beichen{
    public static void main(String[] args){
        int[] arr = new int[]{1,2,3,4,5};
            System.out.println("数组的长度:" + arr.length);
            System.out.println("数组的元素有:")
                for(int i=0; i<arr.length; i++){
                System.out.println(arr[i]);
            }
        }
    }

Default value for eight array elements

Arrays are reference types, and when we use dynamic initialization to create an array, the element values ​​are just default
values.

public class beicheng{
    public static void main(String argv[]){
        int a[]=newint[5];
        System.out.println(a[3]);//a[3]默认值为0
        }
    }

Default initialization values ​​vary for primitive data types.

For reference data types, the default initialization value is null (note that it is different from 0!)
The default initialization value of array element type elements 

the element type of the array

Element default initial value
byte 0
short 0
int 0
long 0L
float 0.0F
double 0.0
char 0
boolean false
reference type null

Nine summary

This is what we wrote in this chapter. In part b, we will update the two-dimensional array. Basically, the part of our array will be updated in the next chapter. As mentioned, the object-oriented part will take a long time, and I will take an exam soon, so the object-oriented part will be very slow, I hope everyone will continue to pay attention

Guess you like

Origin blog.csdn.net/m0_69520030/article/details/131014082
Recommended