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

📄

📁 通过实例可以更好的了解java
💻
📖 第 1 页 / 共 2 页
字号:
26-例子1
import java.util.*;
public class LinkListOne
{public  static void main(String args[])
 { LinkedList mylist=new LinkedList();
   mylist.add("It");          //链表中的第一个节点。
   mylist.add("is");   //链表中的第二个节点。
   mylist.add("a");      //链表中的第三个节点。
   mylist.add("door");    //链表中的第四个节点。
   int number=mylist.size();  //获取链表的长度。
   for(int i=0;i<number;i++)
   {String temp=(String)mylist.get(i);
    System.out.println("第"+i+"节点中的数据:"+temp);
   } 
 }
}



26-例子2
import java.util.*;
public class LinkListTwo
{public  static void main(String args[])
 { LinkedList mylist=new LinkedList();
   mylist.add("is"); mylist.add("a");
   int number=mylist.size();
   System.out.println("现在链表中有"+number+"个节点:");
   for(int i=0;i<number;i++)
   {String temp=(String)mylist.get(i);
    System.out.println("第"+i+"节点中的数据:"+temp);
   }
   mylist.addFirst("It");mylist.addLast("door");
   number=mylist.size(); 
  System.out.println("现在链表中有"+number+"个节点:");
   for(int i=0;i<number;i++)
   {String temp=(String)mylist.get(i);
    System.out.println("第"+i+"节点中的数据:"+temp);
   }
   mylist.remove(0);mylist.remove(1);
   mylist.set(0,"open");
   number=mylist.size();
    System.out.println("现在链表中有"+number+"个节点:");
   for(int i=0;i<number;i++)
   {String temp=(String)mylist.get(i);
    System.out.println("第"+i+"节点中的数据:"+temp);
   }
 }
}



26-例子3
import java.util.*;
class Student
{String name ;int number;float score;
  Student(String name,int number,float score)
  {this.name=name;this.number=number;this.score=score;
  }
}
public class LinkListThree
{public  static void main(String args[])
 { LinkedList mylist=new LinkedList();
   Student stu_1=new Student("赵好民" ,9012,80.0f),
            stu_2=new Student("钱小青" ,9013,90.0f),  
            stu_3=new Student("孙力枚" ,9014,78.0f),
            stu_4=new Student("周左右" ,9015,55.0f);
    mylist.add(stu_1); mylist.add(stu_2);
    mylist.add(stu_3); mylist.add(stu_4);
    Iterator iter=mylist.iterator();
   while(iter.hasNext())
   {  Student te=(Student)iter.next();
      System.out.println(te.name+" "+te.number+"  "+te.score);
   }
 }
}



26-例子4

import java.util.*;import java.awt.event.*;import java.awt.*;
import javax.swing.*;import java.io.*;
class 商品 extends Panel
{String 代号,名称;int 库存;float 单价;
  商品(String 代号,String 名称,int 库存,float 单价)
  {this.代号=代号;this.名称=名称;this.库存=库存;this.单价=单价;
  }
}

class ShowWin extends JFrame implements ActionListener
{  LinkedList goods_list=null;
  JTextField 代号文本框=new JTextField(),
名称文本框=new JTextField(),
            库存文本框=new JTextField(),
单价文本框=new JTextField(),
            删除文本框=new JTextField();
  JButton   b_add=new JButton("添加商品"),
b_del=new JButton("删除商品"),
            b_show =new JButton("显示商品清单");
  JTextArea  显示区=new JTextArea();
  ShowWin()
  {goods_list=new LinkedList();
   Container con=getContentPane(); 
   JScrollPane pane=new JScrollPane(显示区); 
显示区.setEditable(false);
   JPanel save=new JPanel();save.setLayout(new GridLayout(5,2));
   save.add(new Label("输入代号:"));save.add(代号文本框);
   save.add(new Label("输入名称:"));save.add(名称文本框);
   save.add(new Label("输入库存:"));save.add(库存文本框);
   save.add(new Label("输入单价:"));save.add(单价文本框);
   save.add(new Label("点击添加:"));save.add(b_add);
   JPanel del=new JPanel();del.setLayout(new GridLayout(2,2));
   del.add(new Label("输入删除的代号:"));del.add(删除文本框);
   del.add(new Label("点击删除:"));del.add(b_del);
   JPanel show=new JPanel();show.setLayout(new BorderLayout());
   show.add(pane,BorderLayout.CENTER);show.add(b_show,BorderLayout.SOUTH);
   JSplitPane split_one,split_two;
   split_one=new JSplitPane(JSplitPane.VERTICAL_SPLIT,save,del);
   split_two=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,split_one,show);
   con.add(split_two,BorderLayout.CENTER);
   b_add.addActionListener(this);b_del.addActionListener(this);
   b_show.addActionListener(this);
  }
 public void actionPerformed(ActionEvent e)
 {if(e.getSource()==b_add)
  {String daihao=null,mingcheng=null;int kucun=0;float danjia=0.0f;
   daihao=代号文本框.getText();mingcheng=名称文本框.getText();
   kucun=Integer.parseInt(库存文本框.getText());
   danjia=Float.valueOf(单价文本框.getText()).floatValue();
   商品 goods=new 商品(daihao,mingcheng,kucun,danjia);
   goods_list.add(goods);
   try {FileOutputStream file=new FileOutputStream("goods.txt");
        ObjectOutputStream out=new  ObjectOutputStream(file);
        out.writeObject(goods_list);out.close();
       }
   catch(IOException event){}
  }
  else if(e.getSource()==b_del)
  {String daihao=删除文本框.getText();
   try {FileInputStream come_in=new FileInputStream("goods.txt");
        ObjectInputStream in=new  ObjectInputStream(come_in);
        goods_list=(LinkedList)in.readObject();in.close();
       }
   catch(ClassNotFoundException event){}
   catch(IOException event){}
   int number=goods_list.size();
    for(int i=0;i<number;i++)
    {商品 temp=(商品)goods_list.get(i);
      if(temp.代号.equals(daihao)) {goods_list.remove(i);}
      try  {FileOutputStream file=new FileOutputStream("goods.txt");
            ObjectOutputStream out=new  ObjectOutputStream(file);
             out.writeObject(goods_list);
             out.close();
           }
       catch(IOException event){}
      }
  }
 else if(e.getSource()==b_show)
  { 显示区.setText(null);
    try {FileInputStream come_in=new FileInputStream("goods.txt");
        ObjectInputStream in=new  ObjectInputStream(come_in);
        goods_list=(LinkedList)in.readObject();
       }
   catch(ClassNotFoundException event){}
   catch(IOException event){}
   Iterator iter=goods_list.iterator();
   while(iter.hasNext())
    {  商品 te=(商品)iter.next();
       显示区.append("商品代号:"+te.代号+"     ");
       显示区.append("商品名称:"+te.名称+"     ");
       显示区.append("商品库存:"+te.库存+"     ");
       显示区.append("商品单价:"+te.单价+"     ");
       显示区.append("\n");
    }
  }
 }
} 
public class LinkListFour 
{public  static void main(String args[])
 { ShowWin win=new ShowWin();
   win.setSize(100,100);
   win.setVisible(true);
   win.addWindowListener(new WindowAdapter()
      {public void windowClosing(WindowEvent e)
        {System.exit(0);}});
 }
}



26-例子5

import java.util.*;
class StackOne
{public static void main(String args[])
 {Stack mystack=new Stack();
  mystack.push(new Integer(1)); mystack.push(new Integer(2));
  mystack.push(new Integer(3)); mystack.push(new Integer(4));
  mystack.push(new Integer(5)); mystack.push(new Integer(6));
  while(!(mystack.empty()))
  {Integer temp=(Integer)mystack.pop();
   System.out.print("   "+temp.toString());}
 }
}
输出结果是:6 5 4 3 2 1



26-例子6

import java.util.*;
class StackTwo
{public static void main(String args[])
 {Stack mystack=new Stack();
  mystack.push(new Integer(1)); mystack.push(new Integer(1));
  int k=1;
  while(k<=10)
  for(int i=1;i<=2;i++)
   {Integer F1=(Integer)mystack.pop();int f1=F1.intValue();
    Integer F2=(Integer)mystack.pop();int f2=F2.intValue();
    Integer temp=new Integer(f1+f2);
    System.out.println(""+temp.toString()); 
    mystack.push(temp);mystack.push(F2);k++;
   }
 }
}




26-例子7
import java.util.*;
class TreeOne
{public static void main(String args[])
 { TreeSet mytree=new TreeSet();
   mytree.add("boy");mytree.add("zoo");
   mytree.add("apple"); mytree.add("girl");
   Iterator te=mytree.iterator();
   while(te.hasNext())
    System.out.println(""+te.next());
  }
}
输出结果是:
apple
boy
girl
zoo



26-例子8
import java.util.*;import java.awt.*;
class TreeTwo
{public static void main(String args[])
 { TreeSet mytree=new TreeSet(new Comparator()
   {public int compare(Object a,Object b)
    {Student stu1=(Student)a;Student stu2=(Student)b;
     return stu1.compareTo(stu2);}
   });
   Student st1,st2,st3,st4;
   st1=new Student(90,"zhan ying");st2=new Student(66,"wang heng");
   st3=new Student(86,"Liuh qing");st4=new Student(76,"yage ming");
   mytree.add(st1);mytree.add(st2);mytree.add(st3);mytree.add(st4);
   Iterator te=mytree.iterator();
   while(te.hasNext())
    {Student stu=(Student)te.next();
     System.out.println(""+stu.name+"  "+stu.english);}
  }
}
class Student  implements Comparable 
{ int english=0;String name;
 Student(int e,String n)
 {english=e;name=n;
 }
 public int compareTo(Object b)
 { Student st=(Student)b;
   return (this.english-st.english);
 }
}
输出结果是:
wang  heng  66
yage  ming   76
liuhe  qing   86
zhan  ying    90



26-例子9 
import java.util.*;import java.awt.event.*;
import java.awt.*; 

class 节目  implements Comparable
{String name;double time;
  节目(String 名称,double 演出时间)
  {name=名称;time=演出时间;
  }
  public int compareTo(Object b)
  {节目 item=(节目)b;
   return (int)((this.time-item.time)*1000);

⌨️ 快捷键说明

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