数组
数组是应用最广泛的数据结构。它被植入到大部分编程语言中,由于数组十分易懂,所以作为数据结构的起点,并展示面向对象编程和数据结构之间的关系。
Java中数组的基础知识这里就不做赘述。
二分查找(针对有序数组)
public boolean find(int[] arr, int target) {
int low = 0;
int high = arr.length;
int curr;
while (true) {
if (low >= high) {
return false;
}
curr = (low + high) / 2;
if (arr[curr] == target)
return true;
else if (arr[curr] < target) {
low = curr + 1;
} else {
high = curr - 1;
}
//System.out.println("low=" + low + ",high=" + high);
}
}
有序数组
////////////////////////////////////////////////////////////////
class OrdArray {
private long[] a; // ref to array a
private int nElems; // number of data items
// -----------------------------------------------------------
public OrdArray(int max) // constructor
{
a = new long[max]; // create array
nElems = 0;
}
// -----------------------------------------------------------
public int size() {
return nElems;
}
// -----------------------------------------------------------
public int find(long searchKey) {
int lowerBound = 0;
int upperBound = nElems - 1;
int curIn;
while (true) {
curIn = (lowerBound + upperBound) / 2;
if (a[curIn] == searchKey)
return curIn; // found it
else if (lowerBound > upperBound)
return nElems; // can't find it
else // divide range
{
if (a[curIn] < searchKey)
lowerBound = curIn + 1; // it's in upper half
else
upperBound = curIn - 1; // it's in lower half
} // end else divide range
} // end while
} // end find()
// -----------------------------------------------------------
// 在有序数组中插入数据
public void insert(long value) // put element into array
{
int j;
for (j = 0; j < nElems; j++) // find where it goes
if (a[j] > value) // (linear search) 找到第一个比目标数大的数据 从此处开始 后边的每个值顺移
break;
for (int k = nElems; k > j; k--) // move bigger ones up
a[k] = a[k - 1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
// -----------------------------------------------------------
public boolean delete(long value) {
int j = find(value);
if (j == nElems) // can't find it
return false;
else // found it
{
for (int k = j; k < nElems; k++) // move bigger ones down
a[k] = a[k + 1];
nElems--; // decrement size
return true;
}
} // end delete()
// -----------------------------------------------------------
public void display() // displays array contents
{
for (int j = 0; j < nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
// -----------------------------------------------------------
} // end class OrdArray
////////////////////////////////////////////////////////////////
class QueueApp {
public static void main(String[] args) {
int maxSize = 100; // array size
OrdArray arr; // reference to array
arr = new OrdArray(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
int searchKey = 55; // search for item
if (arr.find(searchKey) != arr.size())
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.display(); // display items
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class OrderedApp
----
输出结果:
Found 55
0 11 22 33 44 55 66 77 88 99
11 22 33 44 66 77 88
有序数组的优缺点:
- 查找速度相比无序数组明显加快
- 插入数据变慢(每次插入都需要移动 末尾除外)
- 删除数据变慢(需要移动填补空白)
- 适宜用于频繁查找,不需要插入删除的情况下
数组存储对象
class Person
{
private String lastName;
private String firstName;
private int age;
//--------------------------------------------------------------
public Person(String last, String first, int a)
{ // constructor
lastName = last;
firstName = first;
age = a;
}
//--------------------------------------------------------------
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}
//--------------------------------------------------------------
public String getLast() // get last name
{ return lastName; }
} // end class Person
////////////////////////////////////////////////////////////////
class ClassDataArray
{
private Person[] a; // reference to array
private int nElems; // number of data items
public ClassDataArray(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public Person find(String searchName)
{ // find specified value
int j;
for(j=0; j<nElems; j++) // for each element,
if( a[j].getLast().equals(searchName) ) // found item?
break; // exit loop before end
if(j == nElems) // gone to end?
return null; // yes, can't find it
else
return a[j]; // no, found it
} // end find()
//-------------------------------------------------------------- // put person into array
public void insert(String last, String first, int age)
{
a[nElems] = new Person(last, first, age);
nElems++; // increment size
}
//--------------------------------------------------------------
public boolean delete(String searchName)
{ // delete person from array
int j;
for(j=0; j<nElems; j++) // look for it
if( a[j].getLast().equals(searchName) )
break;
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // shift down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//--------------------------------------------------------------
public void displayA() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
}
//--------------------------------------------------------------
} // end class ClassDataArray
////////////////////////////////////////////////////////////////
class ClassDataApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ClassDataArray arr; // reference to array
arr = new ClassDataArray(maxSize); // create the array
// insert 10 items
arr.insert("Evans", "Patty", 24);
arr.insert("Smith", "Lorraine", 37);
arr.insert("Yee", "Tom", 43);
arr.insert("Adams", "Henry", 63);
arr.insert("Hashimoto", "Sato", 21);
arr.insert("Stimson", "Henry", 29);
arr.insert("Velasquez", "Jose", 72);
arr.insert("Lamarque", "Henry", 54);
arr.insert("Vang", "Minh", 22);
arr.insert("Creswell", "Lucinda", 18);
arr.displayA(); // display items
String searchKey = "Stimson"; // search for item
Person found;
found=arr.find(searchKey);
if(found != null)
{
System.out.print("Found ");
found.displayPerson();
}
else
System.out.println("Can't find " + searchKey);
System.out.println("Deleting Smith, Yee, and Creswell");
arr.delete("Smith"); // delete 3 items
arr.delete("Yee");
arr.delete("Creswell");
arr.displayA(); // display items again
} // end main()
} // end class ClassDataApp
小结
- 数组的缺点:长度不可变
- Java中的数组是对象。
- 无序数组可以快速插入,但是查找和删除较慢。
- 有序数组可以用二分法查找。
- 有序数组查找快,删除、插入慢。