示例代码:
//100以内素数:
- class Prime{
- public static void main(String[] args){
- int i,j;
- boolean isprime;
- for(i=2;i<100;i++){
- isprime=true;
- for(j=2;j<=i/j;j++){
- if((i%j)==0) {
- isprime=false;
- }
- }
- if(isprime) {
- System.out.println(i+" is prime.");
- }
- }
- }
- }
//冒泡排序:
- class Buddle{
- public static void main(String args[]){
- int nums[]={58,99,63,14,2,-7,59595,-89546,452,-455};
- int a,b,t;
- System.out.println("nums数组:");
- for(int i=0;i<nums.length;i++){
- System.out.print(" "+nums[i]);
- }
- System.out.println();
- for(a=1;a<nums.length;a++){
- for(b=nums.length-1;b>=a;b--){
- if(nums[b]<nums[b-1]){
- t=nums[b-1];
- nums[b-1]=nums[b];
- nums[b]=t;
- }
- }
- }
- System.out.println("nums数组排序后:");
- for(int i=0;i<nums.length;i++){
- System.out.print(" "+nums[i]);
- }
- System.out.println();
- }
- }
//队列:
- class Queue{
- char[] q;
- int putloc,getloc;
- public Queue(int size){
- q=new char[size];
- putloc=getloc=0;
- }
- void put(char ch){
- if(putloc==q.length){
- System.out.println("Queue is full");
- return;
- }
- q[putloc++]=ch;
- }
- char get(){
- if(getloc==putloc){
- System.out.println("Queue is empty");
- return (char)0;
- }
- return q[getloc++];
- }
- }
- 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.");
- for(i=0;i<26;i++){
- bigQ.put((char)('A'+i));
- }
- System.out.println("Contents of bigQ:");
- for(i=0;i<26;i++){
- ch=bigQ.get();
- if(ch!=(char)0) System.out.println(ch);
- }
- System.out.println("\n");
- System.out.println("Using smallQ to store the alphabet.");
- for(i=0;i<5;i++){
- //System.out.print("Attemping to store "+(char)('Z'-i));
- smallQ.put((char)('Z'-i));
- System.out.println();
- }
- System.out.println();
- System.out.println("Contents of smallQ:");
- for(i=0;i<5;i++){
- ch=smallQ.get();
- if(ch!=(char)0){
- System.out.print(ch);
- }
- }
- }
- }
//快速排序:
- class Qsort{
- static void qsort(char[] num){
- qs(num,0,(num.length-1));
- }
- private static void qs(char[] num,int left,int right){
- int i,j,t;
- char x,y;
- i = left;
- j = right;
- t = (left + right)/2;
- do{
- x = num[t];
- while((num[i] < x) && (i < right)) i++;
- while((num[j] > x) && (j > left)) j--;
- if(i <= j){
- y = num[i];
- num[i] = num[j];
- num[j] =y;
- i++;
- j--;
- }
- if(i < right){
- qs(num,i,right);
- }
- if(left < j){
- qs(num,left,j);
- }
- }while(i <= j);
- }
- }
- class QsortDemo{
- public static void main(String args[]){
- char[] nums = {'d','g','t','o','p','a','c','f','b','z'};
- char[] chars = {'d','g','t','o','p','a','c','f','h','k'};
- for(char x : nums){
- System.out.print(x +" ");
- }
- System.out.println();
- Qsort.qsort(nums);
- for(char x : nums){
- System.out.print(x +" ");
- }
- System.out.println();
- for(char x : chars){
- System.out.print(x +" ");
- }
- System.out.println();
- Qsort.qsort(chars);
- for(char x : chars){
- System.out.print(x +" ");
- }
- System.out.println();
- }
- }
//堆栈:
- class Stack{
- private char[] stak;
- private int tos;
- Stack(int size){
- stak = new char[size];
- tos = 0;
- }
- Stack(Stack ob){
- stak =new char[ob.stak.length];
- tos =ob.tos;
- for(int i=0;i<tos;i++){
- stak[i] = ob.stak[i];
- }
- }
- Stack(char[] ch){
- stak = new char[ch.length];
- for(int i=0;i<ch.length;i++){
- push(ch[i]);
- }
- }
- void push(char t){
- if(tos == stak.length){
- System.out.println("this is full!!!");
- return;
- }
- stak[tos] = t;
- tos++;
- }
- int pop(){
- if(tos == 0){
- System.out.println("this is empty!!!");
- return (char)0;
- }
- tos--;
- return stak[tos];
- }
- }
- class stackDemo{
- public static void main(String args[]){
- Stack st1 = new Stack(10);
- int i;
- char ch;
- for(i=0;i<10;i++){
- st1.push((char)('A'+i));
- }
- char[] name={'T','o','m'};
- Stack st2 = new Stack(name);
- Stack st3 = new Stack(st1);
- System.out.println("st1:");
- for(i=0;i<10;i++){
- ch =(char) st1.pop();
- System.out.print(ch+" ");
- }
- System.out.println();
- System.out.println("st2:");
- for(i=0;i<3;i++){
- ch = (char)st2.pop();
- System.out.print(ch+" ");
- }
- System.out.println();
- System.out.println("st3:");
- for(i=0;i<10;i++){
- ch = (char)st3.pop();
- System.out.print(ch+" ");
- }
- System.out.println();
- }
- }