java資料結構及演算法二
來源:互聯網
上載者:User
數組線性儲存結構,有無序和有序之;尋找,插入,刪除演算法要考慮數組中是否有重複元素。java中數組基礎java中數組作為對象使用,因此使用new產生對象。 int ar[] = new int[10];數組取值:[]運算子,下標索引從0開始到長度-1;數組初始化:對象為被賦值是預設為null,使用該值會null pointer exception 例外。int ar[] = new int[]{,,,,,};int ar[] = {,,,,,};(初始化列表)// array.java
// demonstrates Java arrays
// to run this program: C>java ArrayApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class ArrayApp
{
public static void main(String[] args) throws IOException
{
int[] arr; // reference
arr = new int[100]; // make array
int nElems = 0; // number of items
int j; // loop counter
int searchKey; // key of item to search for//-------------------------------------------------------------
-
arr[0] = 77; // insert 10 items
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10; // now 10 items in array//-------------------------------------------------------------
-
for(j=0; j<nElems; j++) // display items
System.out.print(arr[j] + " ");
System.out.println("");
//-------------------------------------------------------------
-
searchKey = 66; // find item with key 66
for(j=0; j<nElems; j++) // for each element,
if(arr[j] == searchKey) // found item?
break; // yes, exit before end
if(j == nElems) // at the end?
System.out.println("Can't find " + searchKey); // yes
else
System.out.println("Found " + searchKey); // no
//--------------------------------------------------------------
searchKey = 55; // delete item with key 55
for(j=0; j<nElems; j++) // look for it
if(arr[j] == searchKey)
break;
for(int k=j; k<nElems; k++) // move higher ones
down
arr[k] = arr[k+1];
nElems--; // decrement size
//-------------------------------------------------------------
-
for(j=0; j<nElems; j++) // display items
System.out.print( arr[j] + " ");
System.out.println("");
} // end main()
} // end class ArrayApp以上是過程化的尋找增加刪除。物件導向首先分離資料的儲存結構和資料使用者,從而明確程式的功能,易於理解設計及維護。// lowArray.java
// demonstrates array class with low-level interface
// to run this program: C>java LowArrayApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class LowArray
{
private double[] a; // ref to array a
public LowArray(int size) // constructor
{
a = new double[size];
}
// put element into array
public void setElem(int index, double value)
{
a[index] = value;
}
public double getElem(int index) // get element from array
{
return a[index];
}
} // end class LowArray
////////////////////////////////////////////////////////////////
class LowArrayApp
{
public static void main(String[] args)
{
LowArray arr; // reference
arr = new LowArray(100); // create LowArray object
int nElems = 0; // number of items in array
int j; // loop variable
arr.setElem(0, 77); // insert 10 items
arr.setElem(1, 99);arr.setElem(2, 44);
arr.setElem(3, 55);
arr.setElem(4, 22);
arr.setElem(5, 88);
arr.setElem(6, 11);
arr.setElem(7, 00);
arr.setElem(8, 66);
arr.setElem(9, 33);
nElems = 10; // now 10 items in array
//-------------------------------------------------------------
-
for(j=0; j<nElems; j++) // display items
System.out.print(arr.getElem(j) + " ");
System.out.println("");
//-------------------------------------------------------------//-------------------------------------------------------------
-
int searchKey = 26; // search for data item
for(j=0; j<nElems; j++) // for each element,
if(arr.getElem(j) == searchKey) // found item?
break;
if(j == nElems) // no
System.out.println("Can't find " + searchKey);
else // yes
System.out.println("Found " + searchKey);
//-------------------------------------------------------------
-
// delete value 55
for(j=0; j<nElems; j++) // look for it
if(arr.getElem(j) == 55)
break;
for(int k=j; k<nElems; k++) // move higher ones
down
arr.setElem(k, arr.getElem(k+1) );
nElems--; // decrement size
//-------------------------------------------------------------
-
for(j=0; j<nElems; j++) // display items
System.out.print( arr.getElem(j) + " ");
System.out.println("");
} // end main()
} // end class LowArrayApp以上是對類的簡單的封裝,用來儲存資料的類被稱為容器,不僅儲存資料而且提供存取方法和排序方法等複雜操作。介面:一般指類中的方法的聲明以上類的封裝並不實用,因為setElem和getElem方法仍然執行[]操作,仍是在較低層次上的操作,只是改了個形式而已// highArray.java
// demonstrates array class with high-level interface
// to run this program: C>java HighArrayApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class HighArray
{
private double[] a; // ref to array a
private int nElems; // number of data items
//----------------------------------------------------------
-
public HighArray(int max) // constructor
{
a = new double[max]; // create the array
nElems = 0; // no items yet
}
//----------------------------------------------------------
-
public boolean find(double searchKey)
{ // find specified value
int j;
for(j=0; j<nElems; j++) // for each element,
if(a[j] == searchKey) // found item?
break; // exit loop before end
if(j == nElems) // gone to end?
return false; // yes, can't find it
else
return true; // no, found it
} // end find()
//----------------------------------------------------------
-
public void insert(double value) // put element into array
{
a[nElems] = value; // insert it
- 43 -
nElems++; // increment size
}
//----------------------------------------------------------
-
public boolean delete(double value)
{
int j;
for(j=0; j<nElems; j++) // look for it
if( value == a[j] )
break;
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move higher 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 HighArray
////////////////////////////////////////////////////////////////
class HighArrayApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
HighArray arr; // reference to array
arr = new HighArray(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);
- 44 -
arr.insert(66);
arr.insert(33);
arr.display(); // display items
int searchKey = 35; // search for item
if( arr.find(searchKey) )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class HighArrayApp抽象以上把“怎麼做”從“是什麼”中分離出來,成為抽象。(在一個類中操作怎麼執行,不需要暴露給類的使用者)有序組為什麼使用有序組?二分法增加尋找的速度。有序組可以使用的搜尋方式:線性和二分。線性搜尋:有序組中搜尋方式同無序組,只是查到一個即可完成搜尋。二分尋找:public int find(double 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()// orderedArray.java
// demonstrates ordered array class
// to run this program: C>java OrderedApp
import java.io.*; // for I/O
////////////////////////////////////////////////////////////////
class OrdArray
{
private double[] a; // ref to array a
private int nElems; // number of data items
//----------------------------------------------------------
-
public OrdArray(int max) // constructor
{
a = new double[max]; // create array
nElems = 0;
}
//----------------------------------------------------------
-
public int size()
{ return nElems; }
//----------------------------------------------------------
-
public int find(double 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
- 51 -
} // end find()
//----------------------------------------------------------
-
public void insert(double 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 higher ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//----------------------------------------------------------
-
public boolean delete(double 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 higher 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 OrderedApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
OrdArray arr; // reference to array
arr = new OrdArray(maxSize); // create the array
- 52 -
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有序組的優點:尋找快,插入刪除慢。常用於經常尋找而不常插入和刪除的情況。儲存物件class Person
{
private String lastName;
private String firstName;
private int age;
//----------------------------------------------------------
-
public Person(String last, String first, int a) { // constructor
- 56 -
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
- 58 -
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
- 59 -
// 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