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. 【解决】UEFI+GPT模式下装系统(WIN7/WIN8)

    最近在家想把联想超极本重装系统,可是发现想简单了,预装WIN8的本本用的是UEFI+GPT模式,以前老毛桃装系统那一套不好用了,所以百度了一些方案,还没试,先记着. 1. WIN8 先说装WIN8,貌 ...

  2. ERROR hdfs.DFSClient: Failed to close file解决方法

    14/04/11 17:59:44 ERROR hdfs.DFSClient: Failed to close file /wlan_out/_temporary/_attempt_local_000 ...

  3. 《算法:C语言实现》阅读笔记

    //从今天起准备认真看完这本书.本渣虽然笨,但是窝懒啊.... //今天开始看第一章.希望坚持下去. 第一章 引言 通过讨论连通问题的几种算法,来引出算法的重要性. 1.1 连通问题的快速查找算法 感 ...

  4. 问题-提示“adodataset.command”

    问题现象:提示“adodataset.command” 问题原因:原因不明,希望高人指点. 问题处理:如果报adodataset.command ,如果忽略将删除控件的错误时,你应该可以看一看是不是在 ...

  5. CALayer 的 position和anchorPoint属性

    在iOS 中,UIButton.UIImage等UIView 之所以能够显示在屏幕上,是因为其内部有一个图层(CALayer).通过UIView的layer 属性可以访问这个图层: @property ...

  6. 怎么从代码中拿到栈回溯信息(call stack trace)

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:怎么从代码中拿到栈回溯信息(call stack trace).

  7. bug,不该怕~敢敢test就是了

    转载自:http://bbs.itcast.cn/thread-10103-1-1.html 当程序员的经历让我知道了一些关于软件编程的事情.下面的这些事情可能会让朋友们对软件开发感到惊讶: 一个程序 ...

  8. time_t和struct tm之间的转换

    time_t到struct tm的转换: #include <time.h> struct tm *localtime(const time_t *timep); struct tm到ti ...

  9. careercup-递归和动态规划 9.7

    9.7 编写函数,实现许多图片编辑软件都支持的“填充颜色”功能.给定一个屏幕(以二维数组表示,元素为颜色值).一个点和一个新的颜色值,将新颜色值填入这个点的周围区域,直到原来的颜色值全部改变. 类似l ...

  10. 谈谈JSON数据格式

    JSON 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.        本文主要是对JS操作JSON的要领做下总结.           在JSON中,有两种结构:对 ...