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. oracle to_date函数(转载)

    TO_DATE格式(以时间:2007-11-02   13:45:25为例)           Year:              yy two digits 两位年                ...

  2. Extjs中grid行的上移和下移

    一.将up和down按钮放到tbar中,然后选中grid行即可实现上移和下移 var up = new Ext.Action({ text : 'Up', icon : 'up.png',//或者添加 ...

  3. SQL Server Data Tool 嘹解(了解)一下 SSDT -摘自网络

    Visual Studio 2010 的伺服器管理員可以用來連接 Sharepoint.伺服器,還可以透過資料連接連結至 SQL Server  等資料來源.以資料來源為例,您可以利用單一工具(Vis ...

  4. 由.Net类库提供的农历计算(C#农历)-获取当前日期的农历日期

    ; i <= chineseDate.GetMonthsInYear(DateTime.Now.Year); i++)            {                Console.W ...

  5. 问题-关于sharemem程序访问WEB出现内存错误处理

    [delphi技术] 关于sharemem造成dll错误的处理办法问题现象:如果程序和dll之间用string作为参数传递时容易出现错误问题处理:需要在程序的uses中使用sharemem.这个sha ...

  6. oralce health monitor

    1. Health Monitor简介    Health Monitor是11g里新增加的特性,用于数据库的各层和各个组建的诊断检查.例如可以检查:文件损坏.物理逻辑块损坏.redo和undo故障. ...

  7. 多目标遗传算法 ------ NSGA-II (部分源码解析) 临时种群生成新父代种群 fillnds.c

    /* Nond-domination based selection routines */ # include <stdio.h> # include <stdlib.h> ...

  8. 第一步 配置D2RQ环境

    ​第一步 配置D2RQ环境 Getting Started 2013年10月15日 15:16:55 老套路,还是先配置环境,没查到什么中文资料,写在这里,供大家参考.有需要交流的可以加QQ: 1q7 ...

  9. 【开发工具 - Git】之Git使用案例

    本帖主要介绍一下Git在实际项目开发中的使用流程,包括克隆.暂存.提交.推送.拉取.分支.冲突解决等,暂时还不包括回退. 1.首先需要安装Git,下一步下一步就行,这里就不说了.值得一提的是,如果Gi ...

  10. jQuer基础

    一.概述 jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more.如今,jQuery已经成为最流行的javascript库,在世界前10000个访问最多 ...