将数据推送到堆栈 package michael.week.pkg5; 班级员工{ 字符串Fname; 字符串名称; 字符串PhoneNum; 字符串地址; void setFirst(字符串优先){ Fname=第一; } void setlast(字符串Last){ Lname=最后一个; } 无效设置地址(字符串地址){ 地址=地址; } 无效设置电话(字符串电话){ PhoneNum=电话; } 无效显示(){ System.out.println(“名字是:“+Fname+”,姓氏是:“+Lname+”,地址是:“+address+”,电话是:“+PhoneNum”); } } 包michael.week.pkg5; 导入java.io.BufferedReader; 导入java.io.IOException; 导入java.io.InputStreamReader; 公共课MichaelWeek5{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args)引发IOException{ 类Stck{ 员工stck[]=新员工[10]; int x; void stck(){ x=-1; } 员工推送(员工项目){ 如果(x==9){ System.out.println(“堆栈已满”); }else stck[++x]=项目; 返回stck[x]; } 员工pop(){ if(x0;){ System.out.println(“请输入add以添加新员工”); System.out.println(“请输入pop以弹出最后添加的员工”); System.out.println(“请输入退出以退出”); 字符串选择=br.readLine(); if(选择等于(“添加”)){ 系统输出打印LN(w); 如果(w>=9){ System.out.println(“您达到了最大值!”); 继续; } 否则{ w++; obj.stck(); 字符串信息; System.out.println(“请输入员工姓名:”); info=br.readLine(); System.out.println(“请输入员工姓氏:”); info=br.readLine(); System.out.println(“请输入员工地址:”); info=br.readLine(); System.out.println(“请输入员工电话号码:”); info=br.readLine(); } }else if(choice.equals(“pop”)){ obj.pop(); w--; }else if(选择等于(“退出”)) 打破 else{System.out.println(choice+“是错误的选择!”); } } } }

将数据推送到堆栈 package michael.week.pkg5; 班级员工{ 字符串Fname; 字符串名称; 字符串PhoneNum; 字符串地址; void setFirst(字符串优先){ Fname=第一; } void setlast(字符串Last){ Lname=最后一个; } 无效设置地址(字符串地址){ 地址=地址; } 无效设置电话(字符串电话){ PhoneNum=电话; } 无效显示(){ System.out.println(“名字是:“+Fname+”,姓氏是:“+Lname+”,地址是:“+address+”,电话是:“+PhoneNum”); } } 包michael.week.pkg5; 导入java.io.BufferedReader; 导入java.io.IOException; 导入java.io.InputStreamReader; 公共课MichaelWeek5{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args)引发IOException{ 类Stck{ 员工stck[]=新员工[10]; int x; void stck(){ x=-1; } 员工推送(员工项目){ 如果(x==9){ System.out.println(“堆栈已满”); }else stck[++x]=项目; 返回stck[x]; } 员工pop(){ if(x0;){ System.out.println(“请输入add以添加新员工”); System.out.println(“请输入pop以弹出最后添加的员工”); System.out.println(“请输入退出以退出”); 字符串选择=br.readLine(); if(选择等于(“添加”)){ 系统输出打印LN(w); 如果(w>=9){ System.out.println(“您达到了最大值!”); 继续; } 否则{ w++; obj.stck(); 字符串信息; System.out.println(“请输入员工姓名:”); info=br.readLine(); System.out.println(“请输入员工姓氏:”); info=br.readLine(); System.out.println(“请输入员工地址:”); info=br.readLine(); System.out.println(“请输入员工电话号码:”); info=br.readLine(); } }else if(choice.equals(“pop”)){ obj.pop(); w--; }else if(选择等于(“退出”)) 打破 else{System.out.println(choice+“是错误的选择!”); } } } },java,stack,Java,Stack,您好, 我是java新手,正在开发这个程序……我需要知道如何将数据推送到stck? 注意:push的参数是type employee,employee包含First、Last、Phone和address。如何推送它们? 这就是我所做的,我的导师拒绝了 package michael.week.pkg5; class Employee { String Fname ; String Lname; String PhoneNum; String Ad

您好, 我是java新手,正在开发这个程序……我需要知道如何将数据推送到stck? 注意:push的参数是type employee,employee包含First、Last、Phone和address。如何推送它们? 这就是我所做的,我的导师拒绝了

package michael.week.pkg5;

class Employee {
String Fname ;
        String Lname;
        String PhoneNum;
        String Address;
         void setFirst(String First){
             Fname = First ;
         }
         void setlast(String Last){
             Lname  = Last ;
         }
          void setAddress (String address){
             Address = address ;
         }
         void setPhone (String Phone){
             PhoneNum  = Phone ;
         }          
         void display (){
             System.out.println ("the Fist name is :"+ Fname + "  , the last name is :        " + Lname  + "  ,the address is : "+ Address+ "  ,the phone is : "+ PhoneNum);
         }
         }

    package michael.week.pkg5;

     import java.io.BufferedReader;
     import java.io.IOException;
      import java.io.InputStreamReader;





   public class MichaelWeek5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    class Stck {
    Employee stck [] = new Employee[10];
    int x ;
     void stck (){
        x= -1 ;
    }
    Employee push (Employee item){
        if (x == 9){
            System.out.println ("Stack is full");
        }else stck[++x] = item ;
        return stck[x];
    }
        Employee pop (){
        if (x <0){
            System.out.println (" Stack underflow");
           return stck [x];
        }else 
            return stck[x++];
    }
    }
    InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    String info2 = null ;
    Stck obj = new Stck();
    Employee obj2 = new Employee ();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();
   if(choice.equals("add")){
       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.stck ();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();

       System.out.println ("please enter Employee last name name :");
       info = br.readLine();

       System.out.println ("please enter Employee address :");
       info = br.readLine();

       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       }
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

    } 

   }
package week.pkg5;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
公共课周5{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)引发IOException{
班级员工{
字符串[]Fname=新字符串[10];
字符串[]Lname=新字符串[10];
String[]PhoneNum=新字符串[10];
字符串[]地址=新字符串[10];
int x=-1;
无效增量(){
x++;
}
先空后空(先串){
Fname[x]=第一个;
}
void Pushlast(最后一个字符串){
Lname[x]=最后一个;
}
无效按键电话(字符串电话){
PhoneNum[x]=电话;
System.out.println(“名字是:“+Fname[x]+”,姓是:“+Lname[x]+”,地址是:“+address[x]+”,电话是:“+PhoneNum[x]);
}
无效地址(字符串地址){
地址[x]=地址;
}
void pop(){
if(x<0){
System.out.println(“没有员工!”);
}
否则{
System.out.println(“名字是:“+Fname[x]+”,姓是:“+Lname[x]+”,地址是:“+address[x]+”,电话是:“+PhoneNum[x]);
x--;
}
}
无效显示(){
if(x<0){
System.out.println(“没有员工!”);
}
否则{
对于(int q=0;q 0;){
System.out.println(“请输入add以添加新员工”);
System.out.println(“请输入display以显示所有员工列表”);
System.out.println(“请输入pop以弹出最后添加的员工”);
System.out.println(“请输入退出以退出”);
字符串选择=br.readLine();
if(选择等于(“添加”)){
系统输出打印LN(w);
如果(w>=9){
System.out.println(“您达到了最大值!”);
继续;
} 
否则{
w++;
obj.increment();
字符串信息;
System.out.println(“请输入员工姓名:”);
info=br.readLine();
对象优先(信息);
System.out.println(“请输入员工姓氏:”);
info=br.readLine();
目标推送(信息);
System.out.println(“请输入员工地址:”);
info=br.readLine();
对象地址(信息);
System.out.println(“请输入员工电话号码:”);
info=br.readLine();
对象推送电话(信息);}
}else if(choice.equals(“display”)){
obj.display();
}else if(choice.equals(“pop”)){
obj.pop();
w--;
}else if(选择等于(“退出”))
打破
else{System.out.println(choice+“是错误的选择!”);
}
}
} 
}

您需要创建一个员工堆栈,然后可以将整个员工对象推送到堆栈上

package week.pkg5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Week5 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
      class Employee {


     String [] Fname = new String [10];
        String[] Lname= new String [10];
        String[] PhoneNum= new String [10];
        String[] Address = new String [10];
        int x= -1 ;
         void increment(){
           x++;

        }
         void PushFirst(String First){
             Fname[x] = First ;
         }
         void Pushlast(String Last){
             Lname [x] = Last ;
         }
         void PushPhone (String Phone){
             PhoneNum [x] = Phone ;
             System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
         }
         void PushAddress (String address){
             Address [x] = address ;
         }
         void pop (){
             if (x < 0){
                 System.out.println (" No Empolyee !");
             }
             else {

        System.out.println ("the Fist name is :"+ Fname [x]+ "  , the last name is : " + Lname [x] + "  ,the address is : "+ Address[x] + "  ,the phone is : "+ PhoneNum[x]);
        x--;
        }
}
 void display (){
    if (x < 0){
        System.out.println (" No Empolyee !");
    }
    else {
        for (int q = 0 ; q <=x ; q++){
        System.out.println ((q+1)+"- "+"the First name is :"+ Fname [q]+ "  , the last name is : " + Lname [q] + "  ,the address is : "+ Address[q] + "  ,the phone is : "+ PhoneNum[q]);
        }
    }
}
 }
 InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    Employee obj = new Employee();
    int w = -1 ;
    for (int r=1 ;r>0; ){
   System.out.println("please enter add  to add new employee");
   System.out.println("please enter display  to display all employees list");
   System.out.println("please enter pop to pop the last added employee");
   System.out.println("please enter exit to exit");
   String choice = br.readLine();

   if(choice.equals("add")){


       System.out.println(w);
       if (w >= 9){
           System.out.println("you reached the maxmum number !");
           continue  ;
       } 
       else {
           w++;
           obj.increment();
       String info  ;
       System.out.println ("please enter Employee first name :");
       info = br.readLine();
       obj.PushFirst(info);
       System.out.println ("please enter Employee last name name :");
       info = br.readLine();
       obj.Pushlast(info);
       System.out.println ("please enter Employee address :");
       info = br.readLine();
       obj.PushAddress(info);
       System.out.println ("please enter Employee phone number :");
       info = br.readLine();
       obj.PushPhone(info);}
   }  else if(choice.equals("display")){
           obj.display();
   } else if(choice.equals("pop")){
           obj.pop();
           w--;
   }else if(choice.equals("exit"))
           break ;
       else {System.out.println (choice + " is wrong choice !") ;
   }
    }

} 
 }
import java.util.Stack;
...
员工emp=新员工();
堆栈=新堆栈();
堆栈推送(emp);

在为类say(此处为stck)创建一个对象之后,该类将通过调用下面显示的方法ex来保存数据

import java.util.Stack;
...
Employee emp = new Employee();
Stack<Employee> stack = new Stack<Employee>();
stack.push(emp);
在这里你们可以观察到s被认为是堆栈,我们推了一个pe
class std
{

  int id;
  string name;
  public:
  void set_id(int i)
  {
      id=i;
  }
  void set_name(string n)
  {
      name = n;
   }
  void disp()
  {
     system.out.println("name"+name+" and id = "+id);
  }
 };

  main()
  {
      std s = new std();
      s.set_id(1);
      s.set_name("sunmoon");
      s.disp();
 }
 String [] Fname = new String [10];
 String[] Lname= new String [10];
 String[] PhoneNum= new String [10];
 String[] Address = new String [10];
 int x= -1 ;