Java Applet Getting Started 7

zhaozj2021-02-11  193

Getting started with Java Applet

YY435

Pacific Network Academy

the next day

4. A number of groups

The array is a long-term data structure. The same data type element type is arranged in a certain order, and the array may be an amount of a simple data type, or a class of objects. There are three main features of the array:

1) Array is a collection of elements of the same data type. 2) Each element in the array is in order, and they are continuously stored in the order in memory. 3) Each array element uses the entire name and its own position in the order of the array, such as A [0] represents the first element in the array name A, and A [1] represents an array A The second element, so on.

The Java array requires the following three steps: This has a certain difference than other languages.

1) The declaration array declaration is mainly the name of the data type or element of the declaration of the array of names and an array. There are two kinds of grammatical format of the array: array element types []; array element type [] array name; square bracket [] is an array flag, it can appear behind the array name, or in array elements There is no difference between the two definition methods.

2) Creating an array space declaration array only specifying the name of the array name and array element, and if you want to use the array, you need to open a memory space for it, you create an array space. Like majority, Java does not support arrays that do not define the number of elements, and must specify the length of the array when creating an array space. To determine the size of the exact memory space. The grammatical format is: array name = new array element type [number of array elements]; for example: MyintArray [] = new int [10]; Creating an array of space can also be combined with the declaration, completing a statement, For example: int myintArray = new int [10]; you can also give the initial value when you create an array space, for example: int myintArray [] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; This creates an array containing 10 integer elements, and gives the initial value of each element.

3) Initialization array elements If it is a basic data type, then this step can be omitted itself. Because basic data types have default. You can also initialize it when you create an array space as above. If it is a class, it will be more troublesome. I will say when you are lecture!

Give everyone the following example:

Import java.awt. *;

Import java.applet. *;

Public Class Shuzu Extends Applet

{

INT MyIntarray [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

Public void Paint (Graphics G)

{

g.drawstring ("MyintArray [0] =" MyIntarray [0], 2, 15);

g.drawstring ("MyintArray [1] =" MyIntarray [1], 2, 30);

g.drawstring ("MyintArray [2] =" MyIntArray [2], 2, 45);

g.drawstring ("MyintArray [3] =" MyIntarray [3], 2, 60);

g.drawstring ("MyintArray [4] =" MyIntArray [4], 2, 75);

g.drawstring ("MyintArray [5] =" MyIntarray [5], 2, 90);

g.drawstring ("MyintArray [6] =" MyIntarray [6], 2, 105); g.drawstring ("MyIntArray [7] =" MyIntArray [7], 2,120);

g.drawstring ("MyintArray [8] =" MyIntarray [8], 2, 135);

g.drawstring ("MyintArray [9] =" MyIntarray [9], 2, 150);

}

}

转载请注明原文地址:https://www.9cbs.com/read-4746.html

New Post(0)