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 数组的更多相关文章

  1. Java学习笔记七——数组工具类Arrays

    数组工具类Arrays Java提供的Arrays类里包含的一些static修饰的方法可以直接操作数组.若将里面的方法用熟的话,那开发效率会大大提高.下面介绍其中的方法. List<T> ...

  2. java学习笔记六——数组

    数组类型 数组是一种常见的数据结构,可用于存放多个数据,每一个数组元素存放一个数据,通常可以通过下标进行访问其元素. Java数组要求所有数组元素具有相同的数据类型.因此,数组元素的数据类型是唯一的. ...

  3. [java小笔记] 关于数组内存管理的理解

    数组是大多数编程语言都提供的一种复合结构,如果程序需要多个类型相同的变量时,就可以考虑定义一个数组,java语言的数组变量时引用类型的变量,因此具有java引用变量的特性.在使用数组之前必须对数组对象 ...

  4. java新手笔记15 多态

    1.Animal类 package com.yfs.javase; public class Animal { public void cry() { System.out.println(" ...

  5. 【原】Java学习笔记012 - 数组

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:小店对自己的 ...

  6. 【原】Java学习笔记011 - 数组

    package cn.temptation; import java.util.Scanner; public class Sample01 { public static void main(Str ...

  7. 【原】Java学习笔记010 - 数组

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:一堆分数,要 ...

  8. Java学习笔记day04_数组

    1.switch case switch语句中表达式的数据类型是有要求的: JDK 1.0 ~ 1.4 , 数据类型接受byte, short, int, char JDK 1.5 , 数据类型接受b ...

  9. 1.14(java学习笔记)数组

    假如我们需要用到1000个相同类型的数据,肯定不可能创建1000个变量, 这样既不方便,也不直观,也不便于我们使用.这时就需要用到数组. 一.数组的声明与使用 public class Array { ...

随机推荐

  1. sqlplus中常用设置参数

    一.各种设置参数解释 转自http://baike.baidu.com/view/1239908.htm Sql*plus是一个最常用的工具,具有很强的功能,主要有: 1. 数据库的维护,如启动,关闭 ...

  2. vijosP1210 盒子与球

    vijosP1210 盒子与球 链接:https://vijos.org/p/1210 [思路] Stirling+全排列. 因为第二类stirling所求是没有标明盒子顺序的方案数,所以最后需要乘一 ...

  3. extjs Cannot read property 'dom' of null

    如果你的EXTJS报错: Cannot read property 'dom' of null,那就有可能是因为你的HTML或者JSP文件中的BODY标签里面少了个东西比如代码是: <html& ...

  4. HDU-3622 Bomb Game 2sat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3622 题意:一个平面上有很多的炸弹,每个炸弹的爆炸范围是一样的,求最大的爆炸范围使得炸弹之间不相互影响 ...

  5. c语言 函数返回二位数组 函数参数为二维数组

    通过typedef可以简单实现.也可以直接写. 写了两个简单的矩阵操作的函数简单示例. #include <stdio.h> #include <stdlib.h> const ...

  6. Learning JavaScript Design Patterns The Module Pattern

    The Module Pattern Modules Modules are an integral piece of any robust application's architecture an ...

  7. iOS Xcode的快捷键

    将一些搜集和经常使用的快捷键记录下来,方便你我. Command +1~ 8: 跳转到导航区的不同位置 Command +0 :显示/隐藏导航区 Command Alt 1~ 6:在不同检测器之间跳转 ...

  8. 收集磁盘分区信息(总量、可用、已用、百分比)导出到csv

    #############################脚本功能及说明##################################################该脚本用来收集磁盘分区总大小 ...

  9. S2SH商用后台权限系统第一讲

    各位博友: 您好!从今天开始我们做一套商用的权限系统.功能包含用户管理.角色管理.模块管理.权限管理.大家知道每个商用系统肯定会拥有一套后台系统,我们所讲的权限系统是整个系统核心部分.本套系统技术有s ...

  10. java中synchronized使用方法

    synchronized的一个简单样例 public class TextThread { /**  * @param args  */ public static void main(String[ ...