数组和练习题

一. 练习题

  1. 打印这个图案。

**










**
*

public class day4 {
	/**
	 * @author yhl
	 * @time 2019.03.07
	 * @content recircle
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			for(int i=-6;i<=6;i++){
				for(int j=0;j<=6-Math.abs(i);j++){ 
							System.out.print("*");
				}
				System.out.println( );
		
			}
		}
		}

(备注:如果在打印上下对称的图形时最好使用绝对值的方法还有就是采用从负数到正数的方法;)

  1. 星型图案
    *




public class day4 {
	/**		
	 * @author yhl
	 * @time 2019.03.07
	 * @content recircle
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			for(int i=0;i<=4;i++){
				for(int j=0;j<=4-i;j++){ 
							System.out.print(" ");
				}
				for(int k=1;k<=2*i+1;k++){
				System.out.print("*");
				}
				System.out.println();
			}
			
		}
	}

注意其中有三个:一是行二是空格三是*
一.数组

  1. 定义:具有相同类型数据的集合。
    语法:类型[ ] 变量; 或 类型 变量[ ];
  2. 赋值:变量= new 类型[长度]
    例:int[ ] a;
    a = new int[10];(定义了一个位整型的长度为10的数组)
    合并:int [ ] a = new int[10] ;
    赋值合并:int[ ] a={1,3,5,6};
  3. 下标(索引)
    语法:变量[索引]
  4. 常见方法
    Length长度
  5. 数组是一个引用类型。
    例1. 定义一个5个元素组成的一维数组,对其赋值,再把该数组反向输出。

public class day4 {
/**
* @author yhl
* @time 2019.03.07
* @content recircle
* @param args
*/
public static void main(String[] args) {
int[] arr={1,2,6,9,8};
for(int i=4;i>=0;i–){
System.out.println(arr[i]);
}
}
}

例2. 定义一个包含10个元素的数组,对其进行赋值,使每个元素的值等于其下标

public class day4 {
	/**
	 * @author yhl
	 * @time 2019.03.07
	 * @content recircle
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner  Sc=new Scanner(System.in);
		int a=Sc.nextInt();
		int[] arr=new int[10];
		for(int i=0;i<=9;i++){
			arr[i]=i;
				System.out.print(arr[i]);
			}
				
		}
	}

二.二维数组
类型:类型[ ][ ] 变量={{},{},{}}
例:int[ ][ ] a={{1,5,3},{,=2,6,9},{7}};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值