Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91517
  • 博文数量: 81
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1007
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-20 14:50
文章分类

全部博文(81)

文章存档

2014年(21)

2013年(60)

我的朋友

分类: Java

2013-12-28 14:13:31

  在Java编程语言中,把数组作为对象来看待,因此在创建数组时必须使用new操作符。

  Int[] intArray=new int[100];

  []操作符对于编译器来说是一个标志,它说明正在命名的是一个数组对象而不是普通的变量。

  由于数组是一个对象,所以它的名字是数组的一个引用;它并不是数组本身。数组存储在内存中的其它地址中,而intArray仅仅保存着这个地址。

  数组有一个length字段,通过它可以得知当前数组的大小:

  int arrayLength=intArray.length;

  跟大多数编程语言一样,一旦创建数组,数组大小便不可改变。

  数组中最基本的操作就是插入、查找、删除。

  public class HighArray {

  private long[] a;

  private int nElems;

  /** Creates a new instance of HighArray */

  public HighArray(int max) {

  a=new long[max];

  nElems=0;

  }

  // ...........................................................

  public boolean find(long searchKey)

  {

  int j;

  for(j=0;j

  if(a[j]==searchKey)

  break;

  if(j==nElems)

  return false;

  else

  return true;

  } //end find()

  // ...........................................................

  public void insert(long value){

  a[nElems]=value;

  nElems++;

  }

  // ...........................................................

  public boolean delete(long value)

  {

  int j;

  for(j=0;j

  if(value==a[j])

  break;

  if(j==nElems)

  return false;

  else

  {

  for(int k=j;k

  a[k]=a[k+1];

  nElems--;

  return true;

  }

  } //end delete()

  // ...........................................................

  public void display()

  {

  for(int j=0;j

  System.out.print(a[j]+" ");

  System.out.println("");

  } //end display()

  }

  假 设一个数组,其中的数据项按关键字升序或降序排列,称为有序数组。将数组进行排序的好处是可以通过二分查找显著地提高查找速度。不好的方面是在插入操作中 由于所有靠后的数据都需移动以腾开空间,所以速度较慢。有序数组和无序数组中的删除操作都很慢。这是因为数据项必须向前移动来填补已删除数据项的洞。

  有序数组在查找频繁的情况下非常有用,但若是插入和删除较为频繁时,则无法高效工作。

  public class OrdArray {

  private long[] a;

  private int nElems;

  /** Creates a new instance of OrdArray */

  public OrdArray(int max) {

  a=new long[max];

  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;

  else if(lowerBound>upperBound)

  return nElems;

  else

  {

  if(a[curIn]

  lowerBound=curIn+1;

  else

  upperBound=curIn-1;

  }

  }

  }

  public void insert(long value){

  int j;

  for(j=0;j

  if(a[j]>value)

  break;

  for(int k=nElems;k>j;k--)

  a[k]=a[k-1];

  a[j]=value;

  nElems++;

  }

  public boolean delete(long value){

  int j=find(value);

  if(j==nElems)

  return false;

  else

  {

  for(int k=j;k

  a[k]=a[k+1];

  nElems--;

  return true;

  }

  }

  public void display()

  {

  for(int j=0;j

  System.out.print(a[j]+" ");

  System.out.println("");

  }sdudd1228

  }

  存储对象

  通常我们存储的数据记录是许多字段的集合。在java中一条数据记录经常由一个类对象来表示。例如可以构造一个典型的类来存储一个公司的职员数据。

  public class Person {

  private String lastName;

  private String firstName;

  private int age;

  /** Creates a new instance of Person */

  void Person(String last, String first, int a) {

  lastName=last;

  firstName=first;

  age=a;

  }

  void displayPerson() {

  System.out.print(" Last name: "+lastName);

  System.out.print(", Firstname: "+firstName);

  System.out.print(", Age: "+age);

  }

  public String getLast() {

  return lastName;

  }

  }

  构造函数创建一个新的Person对象并将它的各个字段初始化。DisplayPerson方法显示了一个Person对象的数据。GetLast返回Person的姓;这是用于搜索所需的关键字字段。

  public class Person {

  private String lastName;

  private String firstName;

  private int age;

  /** Creates a new instance of Person */

  public Person(String last, String first, int a) {

  lastName=last;

  firstName=first;

  age=a;

  }

  public void displayPerson() {

  System.out.print(" Last name: "+lastName);

  System.out.print(", Firstname: "+firstName);

  System.out.println(", Age: "+age);

  }

  public String getLast() {

  return lastName;

  }

  }

  public class ClassDataApp {

  /**

  * @param args the command line arguments

  */

  public static void main(String[] args) {

  int maxSize=100;

  ClassDataArray arr;

  arr=new ClassDataArray(maxSize);

  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();

  String searchKey="Stimson";

  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");

  arr.delete("Yee");

  arr.delete("creswell");

  arr.displayA();

  }

  }

阅读(215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~