java - Populate an object array -
I am currently studying Java and it is not certain how to do it.
At this time I am an employee class
public class employee {Private Ent Ampid; Private string empName; Private string job; // Constructors; Public employee () {this.empID = 0; This.empName = ""; This.job = ""; } Public Employee (Anti-APID, String Implement, String Job) {this.empID = empID; This.empName = empName; this. Job = Job; } // it happens; Public int getEmpID () {return.empID; } Public string getEmpName () {return.empName; } Public string getJob () {return this .job; } // set; Public Zero SetEpid (Ent APIDID) {this.empID = empID; } Public Zero Set efmp (string empName) {this.empName = empName; } Public Zero SetJob (string job) {this.job = job; } }
While in my main I created an object and type of employee of type with the size of 2 and then the number of arrays for entering the user Increase in basic information and then enter another loop to print all information.
import java.util.Scanner; Public square employees {public static zero p (string s) {System.out.println (s)}} Public static zero main (string agre []) {Scanners = New Scanner (System.in); Int empID, size = 0, x = 0; String empName; String job; Employee [] employee = new employee [2]; For (size = 0; size & lt; 2; size ++) {p ("employee" + (size + 1)); P ("Enter the employee ID number:"); EmpID = k.nextInt (); K.nextLine (); P ("Please enter your name:"); EmpName = k.nextLine (); P ("Please enter your job role:"); Job = k.nextLine (); Employee [size] = new employee (); Employees [size] .setEmpID (empID); Employees [size] .setEmpName (empName); Employees [size] .setjob (work); } (X = 0; x & lt; 2; x ++) {p ("Hello staff:" + employee [x] .getEmpName () + "Your job is the role" + employees [x] .getJob () + "0800" in your log ID + employee [x] .getEmpID ()); }} }
I understand how to populate each instance of an object with input, but say that I want to increase the size from 2 to 20 But before 10 'pre-admissions' is already, I can currently display 10, but still allows to enter more than 10.
ps sorry for my long-standing questions, poor blocks of code blocks and vocabulary. I'm new to it.
It is strictly necessary when you do not dynamically change their size (and even so In that case you can convert only through ArrayList.toArray (..) ). A ArrayList & lt; Employee & gt; Instead of a employee [] you will not have to worry about the potential, you will not have to worry about the removal and movement of the elements. It will also support normal containers. On the one hand, the names of the upper-case sections are suggested to use Java Naming Convention.
Comments
Post a Comment