java数组 和数组遍历和排序和for while循环冒泡排序

 数组命名和给数组开辟内存空间

/*

数组:一组相同类型数据的集合.可以帮我们将一组数据从0开始编号,便于我们统一管理.

数组是定长的.
查看数组的长度: .length

*/

class ArrayDemo1 {
	public static void main(String[] args) {
		//数组的声明方式一:
		//数据类型[] 数组名
		int[] nums;
		//new 代表开辟内存空间
		nums = new int[5];// new int[长度]
		//int a = 10;
		System.out.println(nums.length);
		System.out.println("-----------------");

		
		nums[0] = 10;
		nums[3] = 40;

		int data = nums[4];
		//System.out.println(data);

		//数组的第二种声明方式
		int[] nums2 = new int[10];
		System.out.println("nums2的长度:" + nums2.length);


		//数组的第三种声明方式
		int[] nums3 = {3,4,5,6,7,8};
		System.out.println(nums3[0]);
		System.out.println(nums3[1]);
		System.out.println(nums3[2]);
		System.out.println(nums3[3]);
		System.out.println(nums3[4]);
		System.out.println(nums3[5]);


		//数组的第四种声明方式
		int[] nums4 = new int[]{1,2,3,4,5,6};
	}
}

 数组遍历

/*

数组的遍历
ArrayIndexOutOfBoundsException:数组下标越界异常
*/

class ArrayDemo2 {
	public static void main(String[] args) {
		int[] nums = {43,1245,24,66,43,789,345};

		for (int i = 0; i < nums.length; i++){//0-7
			System.out.println(nums[i]);
		}

	}
}

 字符串数组

class ArrayDemo3 {
	public static void main(String[] args) {
		
		String[] names = {"张三","李四","王五","赵六"};

		for (int i = 0; i < names.length; i++){
			System.out.println(names[i]);
		}
		

	}
}

 

import java.util.Scanner;
/*
接收5个学员输入的成绩,存入数组中
*/

class ArrayTest1 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int[] scores = new int[5];

		for (int i = 0; i < scores.length; i++){
			
			System.out.print("请输入第" + (i+1) + "个学员的成绩:");
			scores[i] = input.nextInt();
		}

		System.out.println("---------------");
		for (int j = 0; j < scores.length; j++){
			System.out.println(scores[j]);
		}
	}
}
/*

do { 
	循环操作 
}while ( 循环条件 );

*/

class DoWhileDemo1 {
	public static void main(String[] args) {
		
		int i = 1;

		do{
			//循环体,也就是你要重复执行的操作.
			System.out.println("小花花,我爱你!");

			//
			i++;
		}while (i <= 100);
	}
}
class DuoWeiArrayDemo1 {
	public static void main(String[] args) {
		//数组当中数组
		int[][] nums = {{2,3,6},{1,5,8},{4,7,9}};
		/*
		0{2,3,6}
		1{1,5,8}
		2{4,7,9}
		*/

		for (int i = 0; i < nums.length; i++){
			//int[] tempNums = nums[i];
			for (int j = 0; j < nums[i].length; j++){
				System.out.print(nums[i][j] + "    ");
			}
			System.out.println();
		}




	}
}
class ForEachDemo {
	public static void main(String[] args) {
		
		int[] nums = {4,2,6,3,7,8,12};
		/*
		for(数据类型 变量名:数组或集合){
		
		}
		*/
		for(int a:nums){
			System.out.println(a);
		}


		String[] strs = {"哈哈哈","嘻嘻嘻","呵呵呵"};
		for(String str:strs){
			System.out.println(str);
		}

	}
}
class ForForDemo1 {
	public static void main(String[] args) {
			
		for (int i = 1; i <= 3; i++){//外循环控制行数

			for (int j = 1; j <= 4; j++){//内循环控制列数
				System.out.print("*");
			}
			//这行代码只起到换行的作用
			System.out.println("");
			//....
		}


		/*
		j = 1
		j = 2
		j = 3
		j = 4
		
		
		*/


	}
}
/*


*
**
***
****
*****


*/

class ForForDemo2 {
	public static void main(String[] args) {
		
		for (int i = 1; i <= 5; i++){

			for (int j = 1; j <= i; j++){
				System.out.print("*");
			}

			System.out.println("");
		}

	}
}
class FroeachDemo2 {
	public static void main(String[] args) {
		int[][] nums = {{2,3,6},{1,5,8},{4,7,9}};

		for(int[] ns:nums){
			for (int num: ns){
				System.out.print(num + "    ");
			}
			System.out.println();
		}

	}
}

 获取随机数  nextInt(16)   0-15的范围

import java.util.Random;

class RandomDemo {
	public static void main(String[] args) {
		Random r = new Random();

		int num = r.nextInt(16);

		System.out.println(num);
	}
}
/*
1.要接收用户输入的6个红球号码
2.要接收用户输入的蓝球号码

3.系统要随机生成中奖的红球号码
4.系统要随机生成中奖的蓝球号码

5.要比对用户中奖情况
6.要公布中奖结果

*/

class ShuangSeQiuDemo {
	public static void main(String[] args) {
		System.out.println("Hello World!");
	}
}
/*

while:先判断循环条件,如果条件成立,就执行循环操作,然后继续判断循环条件

do while:先执行一遍循环操作,再判断循环条件,如果条件成立,继续执行循环操作.

*/

class WhileAndDoWhileDemo {
	public static void main(String[] args) {
		
		int x = 10;
		while (x <= 5){
			System.out.println("x = " + x);
			x++;
		}

		System.out.println("-----------------------");

		int y = 10;
		do{
			System.out.println("y = " + y);
			y++;
		}while (y <= 5);

		for (int i = 1; i <= 3; i++){
			System.out.println(i);
		}

	}
}

 冒泡排序  遍历数组a    Arrays.toString(a)  要导入import java.util.Arrays

import java.util.Arrays;
public class MaoPaoPx{
	/*
	1 2 3 4 5
	第一轮:
	2 1 3 4 5
	2 3 1 4 5    1   4
	2 3 4 1 5    2   3
	2 3 4 5 1    3   2
	第二轮       4   1
	3 2 4 5 1
	3 4 2 5 1
	3 4 5 2 1
	第三轮
	4 3 5 2 1
	4 5 3 2 1
	第四轮
	5 4 3 2 1
	
	*/
	public static void main(String [] args){
		int a[] = {1,2,3,4,5};
		//外循环轮数
		for(int i=0;i<a.length-1;i++){
			System.out.println("第"+(i+1)+"轮");
			//内循环控制每轮的次数
			for(int j=0;j<a.length-1-i;j++){
				//判断
				int t = 0;
				if(a[j]<a[j+1]){
					t = a[j];
					a[j] = a[j+1];
					a[j+1]=t;
				}
				System.out.println(Arrays.toString(a));
			}
		}
	}
	
}

 数组找最大最小值和平均数

public class MaxPx{
	public static void main(String [] args){
		//声明数组
		int a [] = {1,4,2,8,3,6,9};
		int max = a[0];//声明最大值
		int min = a[0];
		double sum= 0;
		//遍历数组,把数组每一个位置上的数进行比较
		for(int i :a){
			sum+=i;
			//判断
			if(max<i){
				max = i;//获取最大的值
			}
			if(i<min){
				min = i;
			}
		}
		System.out.println("最大值:"+max);
		System.out.println("最小值:"+min);
		System.out.println("平均数:"+sum/a.length);
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值