多維數(shù)組基礎、數(shù)組及循環(huán)練習-創(chuàng)新互聯(lián)

一維數(shù)組:

數(shù)組排序:Arrays.sort(數(shù)組名);(從小到大排序)

10多年的丹徒網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整丹徒建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)從事“丹徒網(wǎng)站設計”,“丹徒網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
package com.demo.cn;

import java.util.Arrays;

public class Demo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[]= {1,2,3,6,7,5,4};
		Arrays.sort(arr);  //從大到小排序
		for(int i=0;i

冒泡排序

package com.demo.cn;
public class Demo3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[]= {1,2,3,6,7,5,4};
		for(int i=0;i
多維數(shù)組(二維數(shù)組,非重點)

概念:java中沒有真正的多維數(shù)組,只是多個一維數(shù)組拼湊。(數(shù)組里面放數(shù)組)

命名規(guī)則:

類型 ? 數(shù)組名【】【】

類型 ? 【】【】數(shù)組名

類型 ? 【】數(shù)組名【】(不推薦使用)

初始賦值:

int arr3【】【】=new int【3】【3】;

int arr4【】【】={{1,2,3},{4,5,6},{7,8,9}};

引用類型:

String str【】【】=new String{ {張三,李四},{王二麻子,小紅}};

二維數(shù)組的遍歷:

package com.demo.cn;
public class Demo4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int arr[][]= {{1,2},{3,6},{7,5}};
		for(int i=0;i
Random ra=new Random(); int a=ra.nextInt(10);? 生成0-9范圍內(nèi)的隨機數(shù)(可在nextInt()+1(生成1-10)); arr3=Arrays.copyOf(arr3,arr3.length+1)(數(shù)組擴容1位); System。out。println(Arrays。toString(arr3));(遍歷數(shù)組arr3) 循環(huán)練習:

package com.demo1.cn;

public class Stident1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int max=11;        //大上半行行數(shù)
		int row=1;           //第row列行 
		int max_star=2*max-1;        //大*數(shù)
	while(row<=max) {                //從第一行到第11行循環(huán)
		int star_con=2*row-1;        //定義*的個數(shù)
		int space_con=max-row;        //定義空格的個數(shù)
		int col=1;
		while(col<=space_con) {        //用while寫出空格的個數(shù)
			System.out.print(" ");    
			col++;
		}
		col=1;
		while(col<=star_con) {        //用while寫出*的個數(shù)
			System.out.print("*");
			col++;
		}
		System.out.println();        //換行
		row++;
		
		
	}
	row=1;
	max=10;
	while(row<=max) {            //下半部分,與上半部分相似
		int star_con=2*max-2*row+1;
		int space_con=row-1;
		int col=0;
		while(col<=space_con) {
			System.out.print(" ");
			col++;
		}
		col=1;
		while(col<=star_con) {
			System.out.print("*");
			col++;
		}
		System.out.println();
		row++;
	}
	}
}

  1. 嵌套循環(huán)

1)//*

//**

//***

//輸出50行

答案:

package com.demo1.cn;

public class Stident2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int row=1;
		while(row<=50) {
			int col=1;
			while(col<=row) {
				System.out.print("*");
				col++;
			}
			System.out.println(" ");
			row++;
		}
		}
}

2)

// ?*

// **

//***

//右對齊,50行

答案:

package com.demo1.cn;

public class Stident3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int row=1;
		while(row<=50) {
			int c=50;
			while(c>row) {
				System.out.print(" ");
				c--;
				
			}
			int col=1;
			while(col<=row) {
				System.out.print("*");
			col++;
			}
			System.out.println();
			row++;
		}
		}
}

3)

*

***

*****

*******

//50行

package com.demo1.cn;

public class Stident4 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int row=1;
		int max=50;
		int max_star=2*max-1;
		while(row<=max) {
			int star_cnt=2*row-1;
			int space_cnt=max-row;
			int col=1;
			while(col<=space_cnt) {
				System.out.print(" ");
				col++;
			}
			col=1;
			while(col<=star_cnt) {
				System.out.print("*");
				col++;
			}
			System.out.println();
			row++;
		}
	}
}

數(shù)組練習:

第一題

// 1.兩個數(shù)組長度都為6,里面放入隨機數(shù)0—9

// 求出兩個數(shù)組中的交集(第一個數(shù)組中有的數(shù)據(jù)第二個數(shù)組也有)

答案:

package com.demo1.cn;

import java.util.Arrays;
import java.util.Random;

public class Shuzu1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a1[]=new int[6];            //定義三個數(shù)組
		int a2[]=new int[6];
		int a3[]=new int [0];
		Random r=new Random();
//		===========給a1和a2生產(chǎn)隨機元素
		for (int i = 0; i< a1.length; i++) {
			a1[i]=r.nextInt(10);//0-9,10個元素
			a2[i]=r.nextInt(10);//從0開始
			
		}
		//============遍歷a1,a2
		for (int i = 0; i< a1.length; i++) {
			System.out.println(a1[i]);
		}
		System.out.println("---------------");
		for (int i = 0; i< a2.length; i++) {
			System.out.println(a2[i]);
		}
		System.out.println("a3============");
		int num=0;
		for (int i = 0; i< a1.length; i++) {        //比較a1,a2數(shù)組內(nèi)相同的元素并寫入a3                    
			for (int j = 0; j< a2.length; j++) {    //數(shù)組中
				if(a1[i]==a2[j]) {
					num=a1[i];
					a3=Arrays.copyOf(a3, a3.length+1);        //用Arrays.copyOf()
					a3[a3.length-1]=num;                        //函數(shù)拓展數(shù)組a3的長度
				}
			}
		}
		//============去除重復元素
		int a4[]=new int [a3.length];
		int t=0;
		for (int i = 0; i< a3.length; i++) {
			boolean bo=true;
			for (int j = i+1; j< a3.length; j++) {
				if(a3[i]==a3[j]) {
				bo=false;
				break;
				}
			}
		
		if(bo) {            //將a3數(shù)組中不重復的數(shù)寫入a4數(shù)組中,并設置t長度
			a4[t]=a3[i];
			t++;
		}
		}
		int a5[]=new int[t];
		System.arraycopy(a4, 0, a5, 0, t);        //將a4數(shù)組中的元素復制到a5中
		System.out.println("");                 //用System.arraycopy函數(shù)
		System.out.println(Arrays.toString(a5));//用Arrays.toString()函數(shù)遍歷數(shù)組a5
	}
	
}

結(jié)果:

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁名稱:多維數(shù)組基礎、數(shù)組及循環(huán)練習-創(chuàng)新互聯(lián)
當前地址:http://www.muchs.cn/article4/dsjsie.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、響應式網(wǎng)站、網(wǎng)站導航標簽優(yōu)化、企業(yè)網(wǎng)站制作、品牌網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)