JAVA一維數組

來源:互聯網
上載者:User

標籤:java.awt   random   介面   介面設計   system   ini   graphics   extend   []   

import java.applet.Applet;import java.awt.*;     //用*表示包含awt這個源包下的所有方法成員,awt為圖形介面設計包public class Applet_Example extends Applet{    int a[];             //定義一個一維數組    public void init(){        a=new int[5];        //定義一個一維數組,該數組包含5個元素        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);  //用圖形介面小視窗輸出    }}
import javax.swing.*;import java.util.*;public class LotteryDrawing {    public static void main(String[] args){        String input=JOptionPane.showInputDialog("請輸入數組的大小(數字)");        int 數組大小=Integer.parseInt(input);        int[] 隨機數組=new int[數組大小];        input=JOptionPane.showInputDialog("請輸入隨機數最大值");        int 最大值=Integer.parseInt(input);        for(int i=0;i<隨機數組.length;i++){            隨機數組[i]=(int)(Math.random()*最大值);   //產生隨機數        }        System.out.println("---------初始狀態--------");        for(int i=0;i<隨機數組.length;i++){            System.out.println("隨機數組["+i+"]="+隨機數組[i]);        }        Arrays.sort(隨機數組);        //數組排序        System.out.println("---------排序後----------");        for(int i=0;i<隨機數組.length;i++)        System.out.println("隨機數組["+i+"]="+隨機數組[i]);    }}
public class 數組 {    public static void main(String[] args){         int[] MyIntArray;         String[] MyStringArray;         char charArray[];         student[] studentArray;         // MyIntArray[0]=32;         //數組建立格式:數組名稱=new 類型[數組大小]         MyIntArray=new int[10];         //MyIntArray[0]=32;         MyStringArray=new String[15];         charArray=new char[20];         studentArray=new student[7];         MyIntArray[0]=0;//數組中第一個元素中賦值0;         MyIntArray[1]=1;//數組中第二個元素中賦值1;         MyIntArray[2]=2;         //MyIntArray[3]=‘abc‘;只能賦值相同類型的值         System.out.println("MyIntArray[2]="+MyIntArray[2]);         int[] a=new int[5];         for(int i=0;i<5;i++)a[i]=i;         for(int i=a.length-1;i>=0;i--)             System.out.print("a["+i+"]="+a[i]+"  ");    }}class student{    String name;    int number;}

 

JAVA一維數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.