⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module5.lst

📁 Mcgraw-Hill - Java 2 - A Beginner S Guide, 2Nd Ed - 2003 -prog.
💻 LST
📖 第 1 页 / 共 2 页
字号:
listing 1
// Demonstrate a one-dimensional array. 
class ArrayDemo {  
  public static void main(String args[]) {  
    int sample[] = new int[10]; 
    int i;  
  
    for(i = 0; i < 10; i = i+1)  
      sample[i] = i; 
 
    for(i = 0; i < 10; i = i+1)  
      System.out.println("This is sample[" + i + "]: " + 
                          sample[i]);  
  }  
}

listing 2
// Find the minimum and maximum values in an array. 
class MinMax {  
  public static void main(String args[]) {  
    int nums[] = new int[10]; 
    int min, max; 
 
    nums[0] = 99; 
    nums[1] = -10; 
    nums[2] = 100123; 
    nums[3] = 18; 
    nums[4] = -978; 
    nums[5] = 5623; 
    nums[6] = 463; 
    nums[7] = -9; 
    nums[8] = 287; 
    nums[9] = 49; 
 
    min = max = nums[0]; 
    for(int i=1; i < 10; i++) { 
      if(nums[i] < min) min = nums[i]; 
      if(nums[i] > max) max = nums[i]; 
    } 
    System.out.println("min and max: " + min + " " + max); 
  }  
}

listing 3
// Use array initializers. 
class MinMax2 {  
  public static void main(String args[]) {  
    int nums[] = { 99, -10, 100123, 18, -978, 
                   5623, 463, -9, 287, 49 }; 
    int min, max; 
 
    min = max = nums[0]; 
    for(int i=1; i < 10; i++) { 
      if(nums[i] < min) min = nums[i]; 
      if(nums[i] > max) max = nums[i]; 
    } 
    System.out.println("Min and max: " + min + " " + max); 
  }  
}

listing 4
// Demonstrate an array overrun. 
class ArrayErr {  
  public static void main(String args[]) {  
    int sample[] = new int[10]; 
    int i;  
  
    // generate an array overrun 
    for(i = 0; i < 100; i = i+1)  
      sample[i] = i; 
  }  
}

listing 5
/* 
   Project 5-1 
   Demonstrate the Bubble sort. 
*/ 
 
class Bubble {  
  public static void main(String args[]) {  
    int nums[] = { 99, -10, 100123, 18, -978, 
                   5623, 463, -9, 287, 49 }; 
    int a, b, t;  
    int size;  
  
    size = 10; // number of elements to sort  
  
    // display original array  
    System.out.print("Original array is:"); 
    for(int i=0; i < size; i++) 
      System.out.print(" " + nums[i]);  
    System.out.println();  
  
    // This is the bubble sort.  
    for(a=1; a < size; a++)  
      for(b=size-1; b >= a; b--) {  
        if(nums[b-1] > nums[b]) { // if out of order  
          // exchange elements   
          t = nums[b-1];  
          nums[b-1] = nums[b];  
          nums[b] = t;  
        }  
      }  
  
    // display sorted array  
    System.out.print("Sorted array is:");  
    for(int i=0; i < size; i++) 
      System.out.print(" " + nums[i]);  
    System.out.println(); 
  } 
}

listing 6
// Demonstrate a two-dimensional array.  
class TwoD {  
  public static void main(String args[]) {  
    int t, i; 
    int table[][] = new int[3][4];  
  
    for(t=0; t < 3; ++t) {  
      for(i=0; i < 4; ++i) {  
        table[t][i] = (t*4)+i+1;  
        System.out.print(table[t][i] + " ");  
      }  
      System.out.println(); 
    }  
  } 
}

listing 7
// Manually allocate differing size second dimensions.  
class Ragged {  
  public static void main(String args[]) {  
    int riders[][] = new int[7][];  
    riders[0] = new int[10];  
    riders[1] = new int[10];  
    riders[2] = new int[10];  
    riders[3] = new int[10];  
    riders[4] = new int[10];  
    riders[5] = new int[2];  
    riders[6] = new int[2];  
  
    int i, j; 
 
    // fabricate some fake data    
    for(i=0; i < 5; i++)   
      for(j=0; j < 10; j++)  
        riders[i][j] = i + j + 10;  
    for(i=5; i < 7; i++)   
      for(j=0; j < 2; j++)  
        riders[i][j] = i + j + 10;  
 
    System.out.println("Riders per trip during the week:"); 
    for(i=0; i < 5; i++) {  
      for(j=0; j < 10; j++)  
        System.out.print(riders[i][j] + " ");  
      System.out.println();  
    } 
    System.out.println(); 
 
    System.out.println("Riders per trip on the weekend:"); 
    for(i=5; i < 7; i++) {  
      for(j=0; j < 2; j++)  
        System.out.print(riders[i][j] + " ");   
      System.out.println();  
    }  
  }  
}

listing 8
// Initialize a two-dimensional array. 
class Squares {  
  public static void main(String args[]) {  
    int sqrs[][] = { 
      { 1, 1 }, 
      { 2, 4 }, 
      { 3, 9 }, 
      { 4, 16 }, 
      { 5, 25 }, 
      { 6, 36 }, 
      { 7, 49 }, 
      { 8, 64 }, 
      { 9, 81 }, 
      { 10, 100 } 
    }; 
    int i, j; 
 
    for(i=0; i < 10; i++) {  
      for(j=0; j < 2; j++)  
        System.out.print(sqrs[i][j] + " ");   
      System.out.println();  
    }  
  }  
}

listing 9
// Assigning array reference variables. 
class AssignARef {  
  public static void main(String args[]) {  
    int i; 
 
    int nums1[] = new int[10]; 
    int nums2[] = new int[10]; 
 
    for(i=0; i < 10; i++) 
      nums1[i] = i; 
 
    for(i=0; i < 10; i++) 
      nums2[i] = -i; 
 
    System.out.print("Here is nums1: "); 
    for(i=0; i < 10; i++) 
      System.out.print(nums1[i] + " ");   
    System.out.println(); 
 
    System.out.print("Here is nums2: "); 
    for(i=0; i < 10; i++) 
      System.out.print(nums2[i] + " ");   
    System.out.println(); 
 
    nums2 = nums1; // now nums2 refers to nums1 
 
    System.out.print("Here is nums2 after assignment: "); 
    for(i=0; i < 10; i++) 
      System.out.print(nums2[i] + " ");   
    System.out.println(); 
 
   // now operate on nums1 array through nums2 
   nums2[3] = 99; 
 
    System.out.print("Here is nums1 after change through nums2: "); 
    for(i=0; i < 10; i++) 
      System.out.print(nums1[i] + " ");   
    System.out.println(); 
  }  
}

listing 10
// Use the length array member.  
class LengthDemo {  
  public static void main(String args[]) {  
    int list[] = new int[10];  
    int nums[] = { 1, 2, 3 };  
    int table[][] = { // a variable-length table  
      {1, 2, 3},  
      {4, 5},  
      {6, 7, 8, 9}  
    };  
  
    System.out.println("length of list is " + list.length);  
    System.out.println("length of nums is " + nums.length);  
    System.out.println("length of table is " + table.length);  
    System.out.println("length of table[0] is " + table[0].length);  
    System.out.println("length of table[1] is " + table[1].length);  
    System.out.println("length of table[2] is " + table[2].length);  
    System.out.println(); 
  
    // use length to initialize list  
    for(int i=0; i < list.length; i++)  
      list[i] = i * i;  
  
    System.out.print("Here is list: "); 
    // now use length to display list  
    for(int i=0; i < list.length; i++)  
      System.out.print(list[i] + " ");  
    System.out.println(); 
  }  
}

listing 11
// Use length variable to help copy an array. 
class ACopy {  
  public static void main(String args[]) {  
    int i; 
    int nums1[] = new int[10]; 
    int nums2[] = new int[10]; 
 
    for(i=0; i < nums1.length; i++) 
      nums1[i] = i; 
 
    // copy nums1 to nums2 
    if(nums2.length >= nums1.length)  
      for(i = 0; i < nums2.length; i++) 
        nums2[i] = nums1[i]; 
 
    for(i=0; i < nums2.length; i++) 
      System.out.print(nums2[i] + " ");   
  } 
}

listing 12
/* 
   Project 5-2 
 
   A queue class for characters. 
*/ 
 
class Queue { 
  char q[]; // this array holds the queue 
  int putloc, getloc; // the put and get indices 
 
  Queue(int size) { 
    q = new char[size+1]; // allocate memory for queue 
    putloc = getloc = 0; 
  } 
 
  // put a characer into the queue 
  void put(char ch) { 
    if(putloc==q.length-1) { 
      System.out.println(" -- Queue is full."); 
      return; 
    } 
     
    putloc++; 
    q[putloc] = ch; 
  } 
 
  // get a character from the queue 
  char get() { 
    if(getloc == putloc) { 
      System.out.println(" -- Queue is empty."); 
      return (char) 0;  
    } 
   
    getloc++; 
    return q[getloc]; 
  } 
} 
 
// Demonstrate the Queue class. 
class QDemo { 
  public static void main(String args[]) { 
    Queue bigQ = new Queue(100); 
    Queue smallQ = new Queue(4); 
    char ch; 
    int i; 
 
    System.out.println("Using bigQ to store the alphabet."); 
    // put some numbers into bigQ 
    for(i=0; i < 26; i++) 
      bigQ.put((char) ('A' + i)); 
 
    // retrieve and display elements from bigQ 
    System.out.print("Contents of bigQ: "); 
    for(i=0; i < 26; i++) {  
      ch = bigQ.get(); 
      if(ch != (char) 0) System.out.print(ch); 
    } 
 
    System.out.println("\n"); 
 
 
    System.out.println("Using smallQ to generate erros."); 
    // Now, use smallQ to generate some errors 
    for(i=0; i < 5; i++) { 
      System.out.print("Attempting to store " + 
                       (char) ('Z' - i)); 
 
      smallQ.put((char) ('Z' - i)); 
 
      System.out.println(); 
    } 
    System.out.println(); 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -