java新手笔记4

1.数组

import java.util.Random;
public class ArrayDemo1 {

	public static void main(String[] args) {
		int a = 10;
		int b = 20;//
		//数组对象  创建10个变量  arr[0]、arr[1]、arr[2]、arr[3]...、arr[9]
		int[] arr = new int[10];//默认0
		arr[0] = 35;
		arr[1] = 29;
		System.out.println("arr[0] = " + arr[0]);
		System.out.println("arr[1] = " + arr[1]);
		//利用for循环访问数组
		for(int i = 0; i < 10; i++ ) {
			System.out.print("arr[1] = " + arr[i] + "\t");
		}
		System.out.println("===================");
		Random ran = new Random();
		for(int i = 0; i < 10; i++ ){
            arr[i] = ran.nextInt(101);
			System.out.println("arr["+i+"] = " + arr[i]);
		}

		//arr[10] = 100;//ArrayIndexOutOfBoundsException: 10
		//arr[9] = 3.14;类型匹配

		Random[] r = new Random[3];
		r[1] = ran;

	}
}

2.改变长度

import java.util.Random;
public class ArrayDemo2 {

	public static void main(String[] args) {
		Random ran = new Random();
		int[] a = new int[8];

		for(int i = 0; i < a.length; i++ ) {
			a[i] = ran.nextInt(41) + 60;
		}

		for(int i = 0; i < a.length; i++ ){
			System.out.println("a["+i+"] = " + a[i]);
		}
		System.out.println("--------------------");
		a = new int[5];//改变长度 length属性

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

	}
}

3.初始化

import java.util.Random;
public class ArrayDemo3 {

	public static void main(String[] args) {
		Random ran = new Random();
		//double[] a = new double[]{3.14, 98, 5.23, 6.14, 100};//数组初始化
		//char[] a = {‘中‘,‘国‘,‘影‘,‘分‘,‘身‘,‘B‘,‘w‘};//创建对象

		/*
		String s = new String("yema");
		String[] a = null;//对象可以null
		//int b = null;
		a = new String[8];
		a[3] = s;
		*/
		String[] a = {"javase","oracle","java web"};

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

	}
}

4.数组遍历

import java.util.Random;
public class ArrayDemo4 {

	public static void main(String[] args) {
		Random ran = new Random();
		int[][] a = new int[3][4];//3行 4列
        //System.out.println("a.length = " + a.length);
		//System.out.println("a[1].length = " + a[1].length);

		//赋值
		for(int i = 0; i < a.length; i++ ){
			for(int j = 0; j < a[i].length; j++) {
               a[i][j] = ran.nextInt(101);
			}
		}
		//输出
		for(int i = 0; i < a.length; i++ ){
			for(int j = 0; j < a[i].length; j++) {
				//System.out.print("a["+i+"]["+j+"] = " + a[i][j] + "  ");
				System.out.print( a[i][j] + "  ");
			}
			System.out.println();
		}

	}
}

5.二维数组

import java.util.Random;
public class ArrayDemo5 {

	public static void main(String[] args) {
		Random ran = new Random();
		int[][] a = new int[3][];//3行 

		a[0] = new int[]{18};
		int[] k = {20,50,90};
		a[1] = k;
		a[2] = new int[]{100,300};

		//输出
		for(int i = 0; i < a.length; i++ ){
			for(int j = 0; j < a[i].length; j++) {

				System.out.print( a[i][j] + "  ");
			}
			System.out.println();
		}

		String[][] s = { //赋初值
			              {"javaEE","hibernate","spring"},
			              {"struts","jquery"}
						};

        s[1][1] = "yema";
        //输出
		for(int i = 0; i < s.length; i++ ){
			for(int j = 0; j < s[i].length; j++) {

				System.out.print( s[i][j] + "  ");
			}
			System.out.println();
		}
	}
}
时间: 2024-10-11 04:53:08

java新手笔记4的相关文章

java新手笔记33 多线程、客户端、服务器

1.Mouse package com.yfs.javase; public class Mouse { private int index = 1; private boolean isLive = false; //跳出方法 同步锁 public synchronized void jump() { while(true) { if(!isLive ) { System.out.println("跳出第 " + index + " 田鼠"); //修改田鼠状态

java新手笔记34 连接数据库

1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcUtil { private static final String driver = "sun.j

java新手笔记10 构造器

1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java.util.Random; public class Demo1 { /** * 模拟摇奖 */ public static void main(String[] args) { Random ran = new Random(); int[] a = new int[7]; System.out.p

java新手笔记15 多态

1.Animal类 package com.yfs.javase; public class Animal { public void cry() { System.out.println("动物叫..."); } } 2.Dog/Cat/Bird类 package com.yfs.javase; public class Dog extends Animal { public void cry() { System.out.println("汪 汪..."); }

java新手笔记1

//Hello.java文件 //类声明 public class Hello{ //声明方法 main程序入口 public static void main (String[] args) { System.out.println("Hello World!"); } } //编译命令 C:\>javac C:\Users\Administrator\Desktop\Hello.java //javac 路径+文件名.java //javac -d D:\ C:\Users\

java新手笔记16 面积

1.图形类 package com.yfs.javase; public class Shape { //计算面积方法 public double getArea() { System.out.println("计算面积"); return 0; } } 2.圆 package com.yfs.javase; public class Circle extends Shape { private double r; public Circle(double r) { this.r =

java新手笔记9

1.bank类 package com.yfs.javase; //类封装 public class BankCard { //属性 int balance;//默认0 实例变量 分配给每个对象一个 //String name; // 方法 存钱 public void saveMoney(int money) { if(money < 0) { System.out.println("非法操作..."); return; } System.out.println("向

java新手笔记2

1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void main(String[] args) { System.out.println("World World!"); //System.out.println("welcome java world!"); //注释的代码不执行 单行注释 /* 多行注释 System.

java新手笔记3

1.包 2.运算符 public class Operator { public static void main(String[] args) { int a = 5; System.out.println("a = " + a); //a = -a; //+ - System.out.println("a = " + a); //+ 字符串链接 System.out.println("影分身" + "软件开发"); Sys

java新手笔记23 异常

1.import package com.yfs.javase; import java.util.Scanner; //import java.lang.String;//默认导入 public class Demo1 { public static void main(String[] args) { String s = new String("abc");//java.lang.String String s1 = "abc"; System.out.pri