04747_java language Programming (i) _ 4th _ Arrays and strings

Source: Internet
Author: User

Example 4.1 A small application declares an array a, creates it in Method init (), specifies 5 elements, and assigns a value to the array element one after the other. Outputs the values of the elements of the array in the method paint ().

Import Java.applet.*;import java.awt.*;p ublic class Example4_1 extends Applet{int a[];//identifier UA can reference an array of element types that are int public void Init () {A=new int[5];//creates an array with 5 elements and lets a refer to it a[0]=100;a[1]=101;a[2]=102;a[3]=103;a[4]=104;} public void Paint (Graphics g) {g.drawstring ("a[0]=" +a[0]+ "a[1]=" +a[1]+ "a[2]=" +a[2],12,12); g.DrawString ("a[3]=" +a[3 ]+ "a[4]=" +a[4],12,32);}}

Example 4.2 set up an array is a reference type of application.

public class Example4_2{static public void Main (string[] args) {int firstarray[]={1,2,3,4};//creates an array of int with array initialization Secondarray []={5,6,7,8,9,10};int myarray[];//Declaration identifier MyArray can reference an array myarray=firstarray;//myarray like Firstarray, Reference the same array System.out.println ("First array:"); for (int index=0;index<myarray.length;index++) {System.out.println ( Myarray[index]);//by MyArray An array of operations}myarray=secondarray;//let MyArray refer to the other arrays System.out.println ("Second array:"); for ( int index=0;index<myarray.length;index++) {System.out.println (Myarray[index]);//operation by MyArray Array}}}

Example 4.3 an application with a triangular two-dimensional array.

public class Example4_3{static public void Main (string[] args) {Boolean btbl[][]=new boolean[4][];//only describes the first dimension, there are 4 sub-arrays for ( int i=0;i<btbl.length;i++)//Create second dimension {btbl[i]=new boolean[i+1];} for (int i=0;i<btbl.length;i++)//Print group contents {for (int k=0;k<btbl[i].length;k++) {System.out.print (btbl[i][k]+ ") on the screen ");} System.out.println ("");}}

Example 4.4 An application that illustrates string connection operations and connection methods.

public class Example4_4{static public void Main (string[] args) {String s1= "ABC"; String s2= "DEFG"; System.out.println (S1+S2);//Apply String connection operation s1= "ABC"; s2= "XYZ"; s1=s1.concat (s2);//Apply string connection method System.out.println (S1);}}

Example 4.5 An application that describes the character substitution of a string and removes whitespace before and after a string.

public class Example4_5{static public void Main (string[] args) {String s= "1234567788", Str;str=s.replace (' 7 ', ' A '); System.out.println ("s=" +s+ "str=" +str); String s2= "123456", Str2;str2=s2.trim (); System.out.println ("s2=" +s2+ "| str2=" +str2+ "|");}}

115615

04747_java language Programming (i) _ 4th _ Arrays and strings

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.