Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the array in Java language?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the array in Java language? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Java array part.

Two common relationships between Java classes are association and dependency. If An is associated with or depends on B, if you only look from A to B, there may be one to one and one to many in terms of quantity. Object-oriented applications map real-world objects and the relationships between them. If we take a closer look at the situation around us, if there are associations or dependencies between objects, 1-to-many relationships are actually more common. For example, a department has multiple employees, a student has multiple departments, and a person has multiple diplomas.

For example, in such an example, students can only choose one free course. If the training center increases the discount, each student can choose up to 3 courses. How can this relationship be maintained?

We might try this:

Package com.csst.relation; public class Student {private String name; / / Note that private Course course1,course2,course3; public Student (String name) {super (); this.name = name;} / / TBD} is modified here

However, next, we will face difficulties in perfecting the chooseCourse,getCourse method. How to know how many courses have been chosen so far? How can I find out the courses that students have chosen at one time?

In other words, when there is an one-to-many quantitative relationship in an association or dependency relationship, we must find a special object to help us, which can wrap up the objects we need to manage and use them as a data container, so that we can easily load some objects in and take them out again. Arrays are one solution. In this article, I will introduce the use of arrays in the Java language.

1. Definition and type of Java array

An array is a collection of elements of the same type. In other words, the data stored in an array is of the same type. First, remember that arrays in Java are reference types. That is, even if all the int data is stored in an array, the type of the array is also a reference type.

2. Declaration of Java array:

If you want to declare an int array, you can declare it in two ways:

Int [] x; int y []

If you want to declare an array of Course types, you can declare it in two ways:

Course [] courses1; Course courses2 []

As you can see, there are two ways to declare an array in the Java language, one is to put [] after the array element type, and the other is to put [] after the array reference name. It is recommended to use * *, that is, int [], Course []. Because the array itself is a data type, that is, from now on, int [], Course [] should be treated as a type, just like String.

3. Initialization of Java array:

Arrays can be initialized using the new keyword, and when initializing with new, the length of the array must be specified in [].

Int [] x=new int [3]; Course [] courses1=new Course [2]

After initializing the array with new, the elements in the array are given default values, for example, the elements in x are all 0quotation courses1 and the elements in null are all defaults.

If you assign an initial value at the same time as the declaration, you can do it in two ways:

Int [] y = {12j 23pm 45}; Course courses2=new Course [] {new Course (), new Course ()}

After assignment, the array y is an array of length 3 and courses2 is an array of length 2.

4. Java multidimensional array

For example, the declaration of a two-dimensional array and the initialization process:

Double [] [] d=new double [3] []; d [0] = new double [4]; d [1] = new double [5]; d [2] = new double [3]; d [0] [0] = 10; d [0] [1] = 100

In the above code, a two-dimensional array is created, which contains three one-dimensional arrays, the length of which is 4pm. According to this syntax and logic, you can also declare three-dimensional, four-dimensional and other multi-dimensional arrays.

5. The length of the Java array

The length of the array is specified at initialization, and once specified, it can no longer be changed, that is, the length of the array is immutable.

Such as:

Int [] x=new int [3]; System.out.println (x.length); / / output 3

Note that the length of the array is taken out using the length attribute instead of the length () method.

6. Traversal method of logarithmic array

Method 1: use the length to control the loop

Int [] x=new int [3]; for (int item0 [I]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report