1. 数组分配了空间,未赋值

public class ArrayDemo01{
public static void main(String args[]){
int score[] = null ; // 声明数组
score = new int[3] ; // 为数组开辟空间,大小为3
System.out.println("score[0] = " + score[0]) ;
System.out.println("score[1] = " + score[1]) ;
System.out.println("score[2] = " + score[2]) ;
for(int x=0;x<3;x++){
System.out.println("score["+x+"] = " + score[x]) ;
}
System.out.println("score[3] = " + score[3]) ;2009-1-19
}
};

运行结果:

score[0] = 0
score[1] = 0
score[2] = 0
score[0] = 0
score[1] = 0
score[2] = 0

2.

public class ArrayDemo02{
public static void main(String args[]){
int score[] = null ; // 声明数组
score = new int[3] ; // 为数组开辟空间,大小为3
for(int x=0;x<3;x++){ // 为每一个元素赋值
score[x] = x * 2 + 1 ; // 每一个值都是奇数
}
for(int x=0;x<score.length;x++){
System.out.println("score["+x+"] = " + score[x]) ;
}
}
};

运行结果:

score[0] = 1
score[1] = 3
score[2] = 5

3.

public class ArrayDemo03{
public static void main(String args[]){
int score[] = null ; // 声明数组
score = new int[3] ; // 为数组开辟空间,大小为3
System.out.println("数组长度为:" + score.length) ;
}
};

运行结果:

数组长度为:3

4.

public class ArrayDemo04{
public static void main(String args[]){
int score[] = {91,92,93,94,95,96} ; // 使用静态初始化声明数组
for(int x=0;x<score.length;x++){ // 循环输出
System.out.println("score["+x+"] = " + score[x]) ;
}
}
};

运行结果:

score[0] = 91
score[1] = 92
score[2] = 93
score[3] = 94
score[4] = 95
score[5] = 96

5.

public class ArrayDemo05{
public static void main(String args[]){
int score[] = {67,89,87,69,90,100,75,90} ; // 使用静态初始化声明数组
int max = 0 ; // 保存数组中的最大值
int min = 0 ; // 保存数组中的最小值
max = min = score[0] ; // 把第一个元素的内容赋值给max和min
for(int x=0;x<score.length;x++){ // 循环输出
if(score[x]>max){ // 依次判断后续元素是否比max大
max = score[x] ; // 如果大,则修改max的内容
}
if(score[x]<min){ // 依次判断后续的元素是否比min小
min = score[x] ; // 如果小,则修改min内容
}
}
System.out.println("最高成绩:" + max) ;
System.out.println("最低成绩:" + min) ;
}
};

运行结果:

最高成绩:100
最低成绩:67

6.

public class ArrayDemo06{
public static void main(String args[]){
int score[] = {67,89,87,69,90,100,75,90} ; // 使用静态初始化声明数组
for(int i=1;i<score.length;i++){
for(int j=0;j<score.length;j++){
if(score[i]<score[j]){ // 交换位置
int temp = score[i] ; // 中间变量
score[i] = score[j] ;
score[j] = temp ;
}
}
}
for(int i=0;i<score.length;i++){ // 循环输出
System.out.print(score[i]+"\t") ;
}
}
};

运行结果:

67 69 75 87 89 90 90 100

7.

public class ArrayDemo07{
public static void main(String args[]){
int score[] = {67,89,87,69,90,100,75,90} ; // 使用静态初始化声明数组
for(int i=1;i<score.length;i++){
for(int j=0;j<score.length;j++){
if(score[i]<score[j]){ // 交换位置
int temp = score[i] ; // 中间变量
score[i] = score[j] ;
score[j] = temp ;
}
}
System.out.print("第" + i + "次排序的结果:") ;
for(int j=0;j<score.length;j++){ // 循环输出
System.out.print(score[j]+"\t") ;
}
System.out.println("") ;
}
for(int i=0;i<score.length;i++){ // 循环输出
System.out.print(score[i]+"\t") ;
}
}
};

运行结果:

第7次排序的结果:67 69 75 87 89 90 90 100

67 69 75 87 89 90 90 100

8.

public class ArrayDemo08{
public static void main(String args[]){
int score[][] = new int[4][3] ; // 声明并实例化二维数组
score[0][1] = 30 ; // 为数组中的内容赋值
score[1][0] = 31 ; // 为数组中的内容赋值
score[2][2] = 32 ; // 为数组中的内容赋值
score[3][1] = 33 ; // 为数组中的内容赋值
score[1][1] = 30 ; // 为数组中的内容赋值
for(int i=0;i<score.length;i++){
for(int j=0;j<score[i].length;j++){
System.out.print(score[i][j] + "\t") ;
}
System.out.println("") ;
}
}
};

运行结果:

0 30 0
31 30 0
0 0 32
0 33 0

9.

public class ArrayDemo09{
public static void main(String args[]){
int score[][] = {
{67,61},{78,89,83},{99,100,98,66,95}
} ; // 静态初始化完成,每行的数组元素个数不一样1
for(int i=0;i<score.length;i++){
for(int j=0;j<score[i].length;j++){
System.out.print(score[i][j] + "\t") ;
}
System.out.println("") ;
}
}
};

运行结果:

67 61
78 89 83
99 100 98 66 95

10.

public class ArrayDemo10{
public static void main(String args[]){
int score[][][] = {
{
{5,1} ,{6,7}
},
{
{9,4},{8,3}
}
} ; // 静态初始化完成,每行的数组元素个数不一样1
for(int i=0;i<score.length;i++){
for(int j=0;j<score[i].length;j++){
for(int k=0;k<score[i][j].length;k++)
{
System.out.println("score["+i+"]["+j+"]["+k+"] = "+score[i][j][k] + "\t") ;
}
}
}
}
};

运行结果:

score[0][0][0] = 5
score[0][0][1] = 1
score[0][1][0] = 6
score[0][1][1] = 7
score[1][0][0] = 9
score[1][0][1] = 4
score[1][1][0] = 8
score[1][1][1] = 3

Java学习--数组的定义和使用的更多相关文章

  1. 《Java基础——数组的定义与使用》

    Java基础--数组的定义与使用       一. 一维数组: 格式一: 数组类型 数组变量[]=new 数据类型[长度]; //需要后续赋值,且后续赋值时只能为单个元素赋值. 或 数组类型 数组变量 ...

  2. C和Java中数组的定义

    在学习C和Java中,关于数组的定义两者不同,在初学的时候,容易产生混淆,现在将两者对比下. 1.初始化 在C语言中,关于一维数组的定义: 完全初始化  int a[5]={1,2,3,4,5},对于 ...

  3. Java基础--数组的定义

    1.数组的定义 数组:一组能够储存相同数据类型值的变量的集合. 2.数组的赋值方式 (1)使用默认的初始值来初始化数组中的每一个元素 语法:数组元素类型[]数组名 = new数组元素类型[数组中元素的 ...

  4. Java中数组的定义与使用(代码+例子)

    学习目标: 掌握一维数组的使用 学习内容: 1.一维数组的定义 数组(Array),是把具有 相同类型 的多个常量值 有序组织 起来的一种数据形式.这些按一定顺序排列的多个数据称为数组.而数组中的每一 ...

  5. java中数组的定义

    1. 一维数组 int[] arr = new int[3];//需要一个容器,但是暂时不给具体的数值 int[] arr = new int[3]{1,2,3};//直接给定具体数值 int[] a ...

  6. java学习——数组

    元素类型[] 数组名 = new 元素类型[元素个数或数组长度]; array 为引用数据类型|-数组数据类型 | 内存结构:程序在运行时,需要在内存中的分配空间.为了提高运行的效率,有对空间进行不同 ...

  7. Java学习---- 数组的引用传递

    1. public class ArrayRefDemo01{ public static void main(String args[]){ int temp[] = {1,3,5} ; // 利用 ...

  8. Java学习-数组

    1.数组的是Object的直接子类,它属于“第一类对象”,但是它又与普通的java对象存在很大的不同,类名为:[I 一维数组:[I 二维数组:[[I 三维数组:[[[I 2.[代表了数组的维度,一个[ ...

  9. Java学习--数组--判断数组中是否包含某个元素的方法

    package zaLearnpackage; import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; import ...

随机推荐

  1. bowtie:短序列比对的新工具

    bowtie:短序列比对的新工具(转) (2014-11-17 22:15:24) 转载▼ 标签: 转载   原文地址:bowtie:短序列比对的新工具(转)作者:玉琪星兆 Bowtie是一个超级快速 ...

  2. Java时代即将来临

    Java语言开发成型的时候有一个构想:就是智能设备互联,笔者推断这个时代即将来临. 我们看看信息时代经历的几个阶段: 机械设备阶段--以算盘.机械式计算机为代表的萌芽阶段. 电子管计算机阶段--以简单 ...

  3. libpcap 库使用(二)

    参考资料: http://www.tcpdump.org/manpages/pcap.3pcap.html 分类介绍了该lib的函数 Opening a capture handle for read ...

  4. 各种编译不通过xcode

    2017-08-24 Apple Mach-O Linker (Id) Error Linker command failed with exit code 1 (use -v to see invo ...

  5. Python 使用for...in...和 while 循环 实现8种格式的 九九乘法表

    #九九乘法表 for...in .. #左下角 for i in range(1,10): for j in range(1,i+1): print(' %d×%d=%2d'%(j,i,i*j), e ...

  6. 【搜索】棋盘问题(DFS)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  7. springMVC学习 六 跳转方式

    SpringMVC的controller中的方法执行完之后,默认的跳转方式是请求转发 如果想要修改跳转方式,可以设置返回值字符串内容(1) 添加 redirect:资源路径 重定向 "red ...

  8. LCA(最近公共祖先)模板

    Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...

  9. markdown中自己偶尔需要的小技巧

    慢慢积累,需要时搜索,并记录与此. 1.写文章时,由于markdown不负责首行缩进,所以“空格”需要特殊的方法去实现,最简单方便的是--输入全角空格(切换全角输入,点空格) 2.markdown中注 ...

  10. 关于oracle的锁表解决session marked for kill

    oracle 使用的过程中,有时候会遇到锁表的情况,数据库增.删.改.查都是会锁表的,但是锁的类型会不同, 大多是行锁,部分会是表锁. 在oracle运行中,一直是有表在锁的,只不过很快一个操作结束, ...