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();
}
}
}
java新手笔记4 数组的更多相关文章
- Java学习笔记七——数组工具类Arrays
数组工具类Arrays Java提供的Arrays类里包含的一些static修饰的方法可以直接操作数组.若将里面的方法用熟的话,那开发效率会大大提高.下面介绍其中的方法. List<T> ...
- java学习笔记六——数组
数组类型 数组是一种常见的数据结构,可用于存放多个数据,每一个数组元素存放一个数据,通常可以通过下标进行访问其元素. Java数组要求所有数组元素具有相同的数据类型.因此,数组元素的数据类型是唯一的. ...
- [java小笔记] 关于数组内存管理的理解
数组是大多数编程语言都提供的一种复合结构,如果程序需要多个类型相同的变量时,就可以考虑定义一个数组,java语言的数组变量时引用类型的变量,因此具有java引用变量的特性.在使用数组之前必须对数组对象 ...
- java新手笔记15 多态
1.Animal类 package com.yfs.javase; public class Animal { public void cry() { System.out.println(" ...
- 【原】Java学习笔记012 - 数组
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:小店对自己的 ...
- 【原】Java学习笔记011 - 数组
package cn.temptation; import java.util.Scanner; public class Sample01 { public static void main(Str ...
- 【原】Java学习笔记010 - 数组
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:一堆分数,要 ...
- Java学习笔记day04_数组
1.switch case switch语句中表达式的数据类型是有要求的: JDK 1.0 ~ 1.4 , 数据类型接受byte, short, int, char JDK 1.5 , 数据类型接受b ...
- 1.14(java学习笔记)数组
假如我们需要用到1000个相同类型的数据,肯定不可能创建1000个变量, 这样既不方便,也不直观,也不便于我们使用.这时就需要用到数组. 一.数组的声明与使用 public class Array { ...
随机推荐
- HW4.2
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW3.10
public class Solution { public static void main(String[] args) { int number1 = (int)(Math.random() * ...
- JDBC与SQL SERVER各个版本的连接方法
转至:blog.csdn.net/ying5420/article/details/4488246 1.SQL SERVER 2000 JDBC驱动程序:msbase.jar.mssqlserver. ...
- IOI1998 hdu1828 poj1177 Picture
写了一发扫描线竟然狂WA不止,hdu死活过不了,poj和当时IOI的数据(还花了我1dsdn积分..)都过了. 然后看到谋篇blog里有评论,把数据拿下来发现WA了. 数据是 20 0 1 11 0 ...
- uedit富文本编辑器
问题: 1: An error occurred at line: 6 in the generated java fileOnly a type can be imported. com.baidu ...
- Cocos2d-x 2.0 自适应多种分辨率
转自:http://dualface.github.io/blog/2012/08/17/cocos2d-x-2-dot-0-multi-resolution/ cocos2d-x 2.0 提供一个极 ...
- 【42】了解typename的双重意义
1.在template声明中,class与typename是等价的,但是使用typename更好. 2.在template实现中,模版形参是从属名称,嵌套在模版形参中的类型是嵌套从属名称,不依赖任何t ...
- ActionBarSherlock
https://github.com/JakeWharton/ActionBarSherlock https://github.com/ddewaele/GoogleMapsV2WithActionB ...
- Jquery 常用总结
获取元素的宽度: 如果用$(ele).attr("width")获取的值不带px 如果用$(ele).css("width")获取的值带px //获 ...
- [GIF] GIF Loop Coder - Introduction
Introducing the program, GIF Loop Coder, which allows you to make looping animated gifs (and other t ...