zoukankan      html  css  js  c++  java
  • Java数据结构的一些基础(更新完成)

    1,输入方法

    java.util.Scanner类

    <pre name="code" class="java">Scanner scan=new Scanner(System.in);

    
    

    通过scan调用下列方法:
    next():读写字符串
    nextInt() :读写整形数据
    nextFloat() :读写浮点
    nextDouble() :

    nextLine() :读取一行数据
    nextByte() :读取字节数据
    2,随机数

    Math.random()表示从0到1内的随机数。
    (int)限定为整型,*2即0->2之间的数,不包括2。
    class createRandom{
     private int random;
     
     public int getRandom(){
      random=(int)(Math.random()*2);
      return random;
      }
     }
     
    public class RandomNumber{
     public static void main(String[] args){
      createRandom cr=new createRandom();
      System.out.println(cr.getRandom());
      }
     }
    

    3.线性表的基础练习,基本操作详细设计

    T1:基本操作结构
    package ch2;
    import java.util.PrimitiveIterator.OfDouble;
    import java.util.Scanner;
    
    import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
    
    import jdk.nashorn.internal.objects.DataPropertyDescriptor;
    public class SeqList {
    	Scanner scan=new Scanner(System.in);
    	private int maxn=100;
    	private int n=-1;
    	private Object[] data;
    	
    	public void initate(){
    		data=new Object[maxn];
    		n=0;
    		System.out.print("初始化完毕");
    		}
    	
    	public void displayData(){
    		if(n<1){
    			System.out.println("线性表无数据");
    			}else{
    				for(int i=1;i<=n;i++){
    					System.out.print(data[i]+" ");
    			}
    		}
    	}
    	
    	public int getSize(){
    		return n;
    	}
    	
    	public void add(Object obj)throws Exception{
    		if(n==-1){
    			throw new Exception("未初始化");
    		}
    		if(n==maxn){
    			throw new Exception("已满");
    		}
    		data[n+1]=obj;
    		n++;
    	}
    	
    	public void insert()throws Exception{
    		System.out.print("请输入要插入第几个数据后面:");
    		int i=scan.nextInt();
    		if(i<0||i>n){
    			throw new Exception("位置错误,请确定插入位置");
    		}
    		if(n==maxn){
    			throw new Exception("已满");
    		}
    		System.out.println("请输入数据:");
    		Object obj=scan.next();
    		for(int j=n;j>i;j--){
    			data[j+1]=obj;
    			n++;
    		}
    	}
    	public Object delete(int i)throws Exception{
    		Object it=data[i];
    		if(i<0||i>n){
    			throw new Exception("位置错误,请确定插入位置");
    		}
    		for(int j=i;j<n;j++){
    			data[j]=data[j+1];
    		}
    		n--;
    		return it;
    	}
    	public Object getData() throws Exception{
    		System.out.print("请输入你要查询的第几个数");
    		int i=scan.nextInt();
    		if(i<0||i>n){
    			throw new Exception("无数据");
    		}
    		System.out.print("你要查找的数据是:"+data[i]);
    		return data[i];
    	}
    	public void updataData(){
    		System.out.print("请输入你要修改的第几个数");
    		int i=scan.nextInt();
    		if(i<=0||i>n){
    			return;
    		}
    		System.out.print("请输入修改的数据为:");
    		Object obj=scan.next();
    		data[i]=obj;
    	}
    }
    

    T2:操作菜单
    package ch2;
    
    import java.util.Scanner;
    public class SeqListMain {
    	public static void main(String []args){
    		SeqList seqlist=new SeqList();
    		   Scanner scan=new Scanner(System.in);
    		   int select;
    		   do{
    			   System.out.println("-----operation------");
    			   System.out.println("1.初始化");
    			   System.out.println("2.显示线性表数据");
    			   System.out.println("3.求线性表数据个数");
    			   System.out.println("4.追加数据");
    			   System.out.println("5.插入数据");
    			   System.out.println("6.删除数据");
    			   System.out.println("7.查找数据");
    			   System.out.println("8.修改数据");
    			   System.out.println("9.退出
    ");
    			   System.out.println("请输入:");
    			   select=scan.nextInt();
    			   switch(select){
    			   case 1:
    				   seqlist.initate();
    				   break;
    			   case 2:
    				   seqlist.displayData();
    				   break;
    			   case 3:
    				   if(seqlist.getSize()==0){
    					   System.out.print("线性表空");
    				   }else{
    					   System.out.print("元素的个数是:");
    					   System.out.print(seqlist.getSize());
    				   };
    				   break;
    			   case 4:
    				   System.out.print("请输入要加入的数据:");
    				   Object obj=scan.next();
    				   try{
    					   seqlist.add(obj);
    				   }catch (Exception e) {
    					   System.out.print(e.toString());
    					// TODO: handle exception
    				}
    				   break;
    			   case 5:
    				   try{
    					   seqlist.insert();
    				   }catch (Exception e) {
    					   System.out.print(e.toString());
    					// TODO: handle exception
    				}
    				   break;
    			   case 6:
    				   System.out.println("请输入你要删除第几个数:");
    				   int i=scan.nextInt();
    				   if(seqlist.getSize()==0){
    					   System.out.print("表空");
    				   }
    				   if(i<0||i>seqlist.getSize()){
    						System.out.print("位置错误,请确定插入位置");
    					}
    				   try{
    					   seqlist.delete(i);
    				   }catch (Exception e) {
    					   System.out.print(e.toString());
    					// TODO: handle exception
    				}
    				   break;
    			   case 7:
    				   try{
    					   seqlist.getData();
    				   }catch (Exception e) {
    					   System.out.print(e.toString());
    					// TODO: handle exception
    				}
    				   break;
    			   case 8:
    				   int number=0;
    				   int index=0;
    				   seqlist.updataData();
    				   break;
    			   case 9:
    				   System.out.print("正在退出");
    				   System.exit(0);break;
    			   }
    		   }while(true);
    	}
    }
    

    4:Java的基本数据结构类

    Collection

    ├List

    │├LinkedList

    │├ArrayList

    │└Vector

    │ └Stack

    └Set

    Map

    ├Hashtable

    ├HashMap

    └WeakHashMap

  • 相关阅读:
    HDU 5744
    HDU 5815
    POJ 1269
    HDU 5742
    HDU 4609
    fzu 1150 Farmer Bill's Problem
    fzu 1002 HangOver
    fzu 1001 Duplicate Pair
    fzu 1150 Farmer Bill's Problem
    fzu 1182 Argus 优先队列
  • 原文地址:https://www.cnblogs.com/pengjunwei/p/3801500.html
Copyright © 2011-2022 走看看