Getting started in the Java language Tutorial (11): Arrays in the Java language

Source: Internet
Author: User
Tags arrays

In tutorial (10), we learned about the two common relationships between Java classes, that is, association and dependency. If a is associated or dependent on B, if only from A to B in this direction, from the number, there may be 1 to 1 and 1 pairs of more than two possible. Object-oriented applications, are mapping the real world of objects and the relationship between objects, and carefully examine the situation around us, objects and objects if there is association or dependence, in fact, more than 1 of the relationship is more common. For example, a department has more than one staff, a student has multiple faculties, a person has more than one diploma ...

The example in the previous article, students can only choose a free course to study, if the training center to increase the strength, each student can choose up to 3 courses to study, how should maintain this relationship?

We may try this:

Package com.csst.relation;
public class Student {
       private String name;
       Note here to modify the
       private Course course1,course2,course3;
       Public Student (String name) {
              super ();
              this.name = name;
TBD
}

But then, to refine the choosecourse,getcourse approach, we will face difficulties. How do you know how many courses have been selected now? How can I find out the courses that students have chosen at once?

In other words, when an association or dependency is in a relationship, when there are 1 pairs of quantitative relationships, we have to find a special object to help, this object can be used to manage the objects we need to package, as a data container to use, so that we can easily put some objects in, it is easy to take out. Arrays are a solution. In this article, you will describe the use of arrays in the Java language.

1, the definition and type of the array

An array is a collection of elements of the same type. That is, the data stored in an array is of one type. First, remember one sentence: The array in Java is a reference type. That is, even if an array has an int data, the type of the array is also a reference type.

2, the Declaration of the array:

Suppose you want to declare an int array, there are two ways to declare it:

Int[] x;

int y[];

Suppose you want to declare an array of course types, there are two ways to declare them:

Course[] courses1;

Course courses2[];

As you can see, there are two ways to declare an array in the Java language, one for [] after the array element type, and one for [] after the array reference name. It is recommended that the first method, namely int[],course[], be used. Because the array itself is a data type, that is, to look at int[],course[from now on, you should treat it as a type, as if you were a string.

Related Article

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.