一维数组

Java的数组是引用类型,c语言的数组是在栈里的

元素为引用数据类型的数组每一个元素都要被实例化。

一维数组的声明方式 Type var[];  eg: int a1[];

什么数组时不能给数组分配长度,eg  int a1[5];   //false

java中使用关键字new 创建数组对象,格式为:

【 数组名 = new 数组元素的类型 [数组元素的个数]】

 

动态初始化和默认初始化

动态初始化:数组定义与为数组元素分配空间和赋值的操作分开进行

默认初始化:数组时引用类型,他的元素相当于类的成员变量,因此每个元素也被按照成员变量的规则被隐式初始化

 

Q:args[]存放的是什么?

public class TestSearch { public static void main(String[] args) { int a[] =
{ 1, 3, 6, 8, 9, 10, 12, 18, 20, 34 }; int i = 12;
System.out.println(binarySearch(a,i)); } public static int binarySearch(int[]
a,int num) { if(a.length == 0) return -1; int startPos = 0; int endPos =
a.length - 1; int m = (startPos + endPos)/2; while(startPos <= endPos) { if(num
== a[m]) return m; if(num > a[m]) { startPos = m+1; } if(num < a[m]) { endPos =
m-1; } m = (startPos + endPos) / 2; } return -1; } } public class Count3Quit2
{ public static void main(String[] args) { KidCircle kc = new KidCircle(500);
int countNum = 0; Kid k = kc.first; while(kc.count > 1) { countNum ++;
if(countNum == 3) { countNum = 0; kc.delete(k); } k = k.right; }
System.out.println(kc.first.id); } } class Kid{ int id; Kid left; Kid right; }
class KidCircle{ int count = 0; Kid first,last; KidCircle(int n){ for(int i=0;
i<n; i++) { add(); } } void add() { Kid k = new Kid(); k.id = count; if(count
<= 0) { first = k; last = k; k.left = k; k.right = k; }else { last.right = k;
k.left = last; k.right = first; first.left = k; last = k; } count ++; } void
delete(Kid k) { if(count <= 0) { return; }else if(count == 1) { first = last =
null; }else { k.left.right = k.right; k.right.left = k.left; if(k == first) {
first = k.right; } else if( k == last) { last = k.left; } } count --; } }
学习面向对象的过程

二维数组

二维数组可以看成以数组为元素的数组。eg:

int a[][] = { {1,2},{3,4,5},{6,7}};

java中多维数组的声明和初始化应该按从高维到低维的顺序进行,
int a[][] = new int[3][]; a[0] = new int[2]; a[1] = new int[3]; a[2] = new
int[2]; int t1[][] = new int [][4]; //false

 
import javax.xml.crypto.Data; public class TestDateSort { public static void
main(String[] args) { Date[] days = new Date[5]; days[0] = new Date(2006,5,4);
days[1] = new Date(2006, 7, 4); days[2] = new Date(2008, 5, 4); days[3] = new
Date(2004, 5, 9); days[4] = new Date(2004, 5, 4); Date d = new Date(2006,7,4);
String str = String.valueOf(d); bubbleSort(days); for(int
i=0;i<days.length;i++) { System.out.println(days[i]); }
System.out.println(binarySearch(days,d)); } public static Date[]
bubbleSort(Date[] a) { int len = a.length; for(int i = len-1;i>=1;i--) {
for(int j = 0;j<=i-1;j++) { if(a[j].compare(a[j+1]) > 0) { Date temp = a[j];
a[j]=a[j+1]; a[j+1]=temp; } } } return a; } public static int
binarySearch(Date[] days,Date d) { if (days.length==0) return -1; int startPos
= 0; int endPos = days.length-1; int m = (startPos + endPos) / 2;
while(startPos <= endPos){ if(d.compare(days[m]) == 0) return m;
if(d.compare(days[m]) > 0) { startPos = m + 1; } if(d.compare(days[m]) < 0) {
endPos = m -1; } m = (startPos + endPos) / 2; } return -1; } } class Date { int
year,month,day; Date(int y,int m,int d){ year = y; month = m; day = d; } public
int compare(Date date) { return year > date.year ? 1 :year < date.year? -1
:month > date.month ? 1 : month < date.month ? -1 : day > date.day ? 1 : day <
date.day ? -1 : 0; } public String toString() { return "Year:Month:Day -- " +
year + "-" + month + "-" + day; } }
数组的拷贝

public static void arraycopy(Object src, int srcPos, Object dest.int
destPos,int length)

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信