Java One-dimensional arrays

Source: Internet
Author: User

ImportJava.applet.Applet;Importjava.awt.*;//use * to represent all method members under the source package, AWT for the graphical interface design package Public classApplet_exampleextendsapplet{intA[];//define a one-dimensional array     Public voidInit () {a=New int[5];//defines a one-dimensional array that contains 5 elementsa[0]=100;//assigning values to array elements, respectivelyA[1]=101; a[2]=102; a[3]=103; a[4]=104; }     Public voidPaint (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);//small window output with graphical interface    }}
Importjavax.swing.*;ImportJava.util.*; Public classlotterydrawing { Public Static voidMain (string[] args) {String input=joptionpane.showinputdialog ("Please enter the size of the array (number)"); intArray size =integer.parseint (input); int[] Random array =New int[array size]; Input=joptionpane.showinputdialog ("Please enter the maximum number of random numbers"); intMaximum value =integer.parseint (input);  for(inti=0;i< random Array. length;i++) {Random array [i]=(int) (Math.random () * maximum value);//Generate random numbers} System.out.println ("---------Initial state--------");  for(inti=0;i< random Array. length;i++) {System.out.println ("Random Array [" +i+ "]=" +random Array [i]);        } arrays.sort (random array); //Array SortingSystem.out.println ("-------------------after sorting");  for(inti=0;i< random Array. length;i++) System.out.println ("Random Array [" +i+ "]=" +random Array [i]); }}
 Public classArray { Public Static voidMain (string[] args) {int[] myintarray;         String[] myStringArray; Charchararray[];         Student[] Studentarray; //myintarray[0]=32; //Array creation format: array name =new type [array size]myintarray=New int[10]; //myintarray[0]=32;mystringarray=NewString[15]; Chararray=New Char[20]; Studentarray=NewStudent[7]; myintarray[0]=0;//The first element in the array is assigned a value of 0;Myintarray[1]=1;//The second element in the array is assigned a value of 1;myintarray[2]=2; //myintarray[3]= ' abc '; only values of the same type can be assignedSystem.out.println ("myintarray[2]=" +myintarray[2]); int[] A=New int[5];  for(inti=0;i<5;i++) a[i]=i;  for(inti=a.length-1;i>=0;i--) System.out.print ("a[" +i+ "]=" +a[i]+ ""); }}classstudent{String name; intNumber ;}

Java One-dimensional arrays

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.