一、数组的创建和初始化

  (一)创建数组:

import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//new 数组元素类型[元素个数]
int [] s = new int [2];
s[0] = 1;
s[1] = 2; //对象数组:存放对象的引用
Student [] t = new Student[2];
t[0] = new Student(201901010101l,"li");
t[1] = new Student(201901060326l,"King");
for(int i = 0 ; i < t.length ; i++)
{
System.out.println(t[i].toString());
}
} }
class Student
{
long no;
String name; public Student()
{
super();
} public Student(long no, String name)
{
this.no = no;
this.name = name;
}
@Override
public String toString()
{
return "no : "+no+
" name : "+name;
} }

  (二)数组初始化

    1.动态初始化:数组的定义与为数组分配空间、赋值操作分开进行。

      -先分配空间,然后给元素赋值

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int[] a ;
a = new int [2];
a[1] = 1;
a[2] = in.nextInt();
}

    2.静态初始化:定义数组的同时给数组分配空间并赋初值

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int [] a1 = {1,in.nextInt()};
int [] a2 = new int []{1,2,3,4,5};
}

  (三)数组创建时的默认初始化

    int : 0

    double : 0.0

    String : null

    对象: null

二、数组的使用

  1.必须用new为数组分配空间后,才可以引用数组中的元素

  2.引用数组元素的方法:a[i] i取值0~n-1

  3.属性:length(只读),数组长度

  4.数组创建后不能改变大小,但可运行时动态确定数组的大小

三、二维数组

  1.Java中实际上没有多维数组,只有一维数组

  2.本质:数组的数组

  3.可以有不规则数组:不同行可以有不同长度

  4.二维数组的创建与遍历

import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a[][] = new int[3][];
a[0] = new int [2];
a[0][0] = 1;
a[0][1] = 2;
a[1] = new int []{1,2,3};
a[2] = new int [] {1,2,3,4};
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();
}
}
}

四、数组的工具类

1.数组的拷贝(按块拷贝)

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a[] = {1,2,3};
int b[] = new int [3];
//System.arraycopy(from, fromindex, to, toIndex, count);
System.arraycopy(a, 0, b, 0, b.length);
for(int i = 0 ; i < b.length ; i++)
{
System.out.print(b[i]+" ");
}
}

2.Arrays

  java.util.Arrays

  功能:定义了多种数组操作静态方法,实现了对数组元素的排序、填充、转换为列表或字符串形式、增强的检索和深度比较等功能

  

import java.util.Arrays;
public class Main
{
public static void main(String[] args)
{
int a[] = {1,2,3,39,0,19,89}; System.out.print(Arrays.toString(a));//转换为列表或字符串形式
Arrays.sort(a);//数组元素排序 System.out.println();
System.out.println("-----------"); System.out.print(Arrays.toString(a)); System.out.println();
System.out.println("-----------"); System.out.println(Arrays.binarySearch(a, 19));//二分查找根据元素锁定下标
} }

最新文章

  1. scrapy 知乎用户信息爬虫
  2. c#与js中10进制16进制的转化,记录防忘
  3. C语言第2天基本运算
  4. android 使用静态变量传递数据
  5. ASP.NET Core 行军记 -----第一步(艰辛的 MVC Hello World)
  6. jQuery弹出层_点击自身以外地方关闭弹出层
  7. 【转】使用 vim + ctags + cscope + taglist 阅读源码
  8. 用C语言打印出三角函数
  9. 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
  10. Android文本Flood it游戏源代码
  11. mongodb集群+分片部署(二)
  12. 我也谈javascript正则匹配
  13. python基础阶段 经典练习题 拾英札记(2)
  14. python访问redis
  15. JAVA 中的命名规则
  16. 重复造轮子,编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)
  17. Scala变量| 流程控制
  18. session和cookie知识点总结
  19. openstack--3--控制节点安装配置keystone
  20. Unity3D-RPG项目实战(4):角色性能測试

热门文章

  1. MySQL同步部分库注意的问题
  2. 语言-页面-模板-thymeleaf
  3. antDesignVue表格
  4. java 类对象四种方法加载方式
  5. yii 自定义form样式适应现成模板
  6. web自动化测试python+selenium----API
  7. 注释中的Unicode编码也会被转义
  8. PYTHON常用五大库
  9. C++ PTA 本题要求实现一个计算m和n之间所有整数的和
  10. 用js获取当前路由信息的方法