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