java 数组

本文最后更新于 2020年5月18日 晚上

基础

声明

可以这样声明:

1
2
int[] scores;
String[] names;

也可以这样声明

1
2
int scores[];
String names[];

二维更为古怪
1
2
3
int [][]x;
int []x[];
int x[][];

这里注意一点,声明时不能往括号中加东西,会报错。例如:int x[1];//报错

java中推荐吧括号放到前面,可能int[]也成了一个对象?

创建数组对象

创建数组对象语法和c++中创建动态数组类似。

int[] scores = new int[100];

上面这个代码首先要在堆中分配空间,然后把里面的数据初始化。

括号中的数字可以使常量,也可以是变量,甚至可以是0(表示里面没有数据).

访问数组的元素和长度

和c++一样,下标索引。如果越界,会抛出ArrayIndexOutOfBoundsException异常

所有数组都有length属性,表示数组的长度: public final length.

所以我们可以直接输出这一属性,例如:

1
2
int x[] = new int[40];
System.out.println(x.length); //输出length

如果是对象数组,那么数组中的元素时对象的引用,因此当我们不使用数组的时候最好把值赋null触发垃圾回收机制。

1
2
3
4
5
String sb = new String("a");
String sbs[] = new String[](sb,null);
...
sbs[0] = null;//清除数组对象的引用
sbs = null;//清除数组引用

多维数组和不规则数组

1
2
3
String[][] rooms = new String[2][];
rooms[0] = new String[]("Tom","Mike");
rooms[1] = new String[]("Mary");

可以用不等长数组是因为每一行都是一个数组元素,都有自己的length变量

1
2
3
4
5
6
7
8

for(int i=0; i<rooms.length; i++)
{
for(int k=0; k<rooms[i].length; k++)
{
System.out.println(rooms[i][k]);
}
}

第一个循环的length是rooms引用多少个数组,rooms[]指的是每个数组的长度。

要注意只有最后一个括号可以不加数字,我的理解是只有最后一层才是真正的数组对象,才有length,同时,前面不确定就不知道要开多少个数组对象。

数组作为返回值

在c++中数组是不能作为返回值的,因为c++中数组是一个指针,而又不能像其他指针一样确定它的大小。而在java中数组是一个对象,可以返回它的引用。

哈希表

平常我们想找某一个值都是通过遍历数组得到的,我们也可以用值通过某种映射关系得到在数组中的位置。这就是哈希表

例如:

1
2
3
4
public int hash(int value)
{
return value%10-1;
}

上面这个例子中value就是值,返回的是下标。但是这时如果超过十就会出现重复,这叫做哈希冲突。我们可以设计更复杂的映射来处理哈希冲突。

判断重复的条件是 object1.equals(boject2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class Node
{
private Object value;
private Node next;

public Node(Object value)
{
this.value = value;
}
public Object getValue()
{
return value;
}
public Node getNext()
{
return next;
}
public void setNext(Node next)
{
this.next = next;
}
}

public class MyhashSet
{
private Node[] array;
private int size = 0;//数组元素数目
public MyHashSet(int length)
{
array = new Node[length];
}
public int size()
{
return size;
}

private static int hash(Object o)//哈希算法
{
int h = o.hashCode();//Object类中有这个方法
h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;
}

private int indexFor(int hashCode)
{
return hashCode & (array.length-1);//返回索引
}

public void add(Object value)
{
int index = indexFor(hash(value));
Node newNode = new Node(value);
Node node = array(index);
if(node == null)
{
array(index) = newNode;
size++;
}
else//处理哈希冲突
{
Node nextNode;
while(!node.getValue().equals(value) &&
(nextnode = node.getNext()) != null)
{
node = nextNode;
}
//不允许加入重复元素
if(!node.getValue().equals(value))
{
node.setNext(newNode);
size++;
}
}
}
//测试是否有这个对象
public boolean contains(Object value)
{
int index = indexFor(hash(value));
Node node = array[index];
while(node != null && !node.getValue().equals(value))
{
node = node.getNext();
}
if(node!=null && node.getValue().equals(value))
{
return true;
}
else
{
return false;
}
}

public boolean remove(Object value)
{
int index = indexFor(hash(value));
Node node = array[index];
if(node!=null && node.getValue().equals(value))
{
array[index] = node.getNext();
size--;
return true;
}

Node lastNode = null;
while(node != null && !node.getValue().equals(value))
{
lastNode.setNext(node.getNext());
size--;
return true;
}
else
{
return false;
}
}
public Object[] getAll()
{
Object[] values = new Object[size];
int index = 0;
for(Node node: array)
{
while(node != null)
{
values[index++] = node.getValue();
node = node.getNext();
}
}
return values;
}
}

Arrays类

java.util.Arrays类,有一系列操作数组的方法。这是一个私有类

静态方法:

  • equals(): 比较两个数组是否相同
  • fill(): 向数组中填充数据
  • sort(): 把数组升序排列
  • parallelSort(): 开启多个线程,以并发的方式对数组中元素进行排序,提高效率
  • asList()把一个数组变成List

例: Arrays.sort(a);

用 … 声明数目可变参数

可以用int… a代替 int[] a;这种模式下如果输入立即数可以转化成数组类型:

1
2
3
4
5
6
7
8
public static int max(int... datas)
{
...
}
public static void main(String[] args)
{
System.out.println(max(5, 8, 2, 4, 5);//可以直接输入数据
|

但是必须要放在参数列表的最后一位

```
pubilc void max(int… data, String p)//错误,int… 必须放在最后
{

}


java 数组
https://www.xinhecuican.tech/post/325cdbf2.html
作者
星河璀璨
发布于
2020年5月14日
许可协议