资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

多维数组基础、数组及循环练习-创新互联

一维数组:

数组排序:Arrays.sort(数组名);(从小到大排序)

10多年的丹徒网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整丹徒建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“丹徒网站设计”,“丹徒网站推广”以来,每个客户项目都认真落实执行。
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
多维数组(二维数组,非重点)

概念:java中没有真正的多维数组,只是多个一维数组拼凑。(数组里面放数组)

命名规则:

类型   数组名【】【】

类型   【】【】数组名

类型   【】数组名【】(不推荐使用)

初始赋值:

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

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

引用类型:

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

二维数组的遍历:

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范围内的随机数(可在nextInt()+1(生成1-10)); arr3=Arrays.copyOf(arr3,arr3.length+1)(数组扩容1位); System。out。println(Arrays。toString(arr3));(遍历数组arr3) 循环练习:

package com.demo1.cn;

public class Stident1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int max=11;        //大上半行行数
		int row=1;           //第row列行 
		int max_star=2*max-1;        //大*数
	while(row<=max) {                //从第一行到第11行循环
		int star_con=2*row-1;        //定义*的个数
		int space_con=max-row;        //定义空格的个数
		int col=1;
		while(col<=space_con) {        //用while写出空格的个数
			System.out.print(" ");    
			col++;
		}
		col=1;
		while(col<=star_con) {        //用while写出*的个数
			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. 嵌套循环

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++;
		}
	}
}

数组练习:

第一题

// 1.两个数组长度都为6,里面放入随机数0—9

// 求出两个数组中的交集(第一个数组中有的数据第二个数组也有)

答案:

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];            //定义三个数组
		int a2[]=new int[6];
		int a3[]=new int [0];
		Random r=new Random();
//		===========给a1和a2生产随机元素
		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数组內相同的元素并写入a3                    
			for (int j = 0; j< a2.length; j++) {    //数组中
				if(a1[i]==a2[j]) {
					num=a1[i];
					a3=Arrays.copyOf(a3, a3.length+1);        //用Arrays.copyOf()
					a3[a3.length-1]=num;                        //函数拓展数组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数组中不重复的数写入a4数组中,并设置t长度
			a4[t]=a3[i];
			t++;
		}
		}
		int a5[]=new int[t];
		System.arraycopy(a4, 0, a5, 0, t);        //将a4数组中的元素复制到a5中
		System.out.println("");                 //用System.arraycopy函数
		System.out.println(Arrays.toString(a5));//用Arrays.toString()函数遍历数组a5
	}
	
}

结果:

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站名称:多维数组基础、数组及循环练习-创新互联
分享URL:http://cdkjz.cn/article/dsjsie.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220