java array question?

Hi am new to java programming am looking for a way to set an array to an array.. below what i want to do is for each employee i want it to be set to an ID in the emp_id array.. i am unsure of how to do this... so i want like emp_names[0] to be assigned to emp_id[0]. 

 String[] emp_names=new String[10];






int[] emp_id=new int[10];

 emp_id [0]= 10061;

 emp_id [1]=10103;

 emp_id [2]= 10111;

    emp_id [3]= 10133;

 emp_id [4]=0;

1 Answer

  • 1 month ago

    You seem to have vocab difficulties here.  "Assigning" is what you're already doing: giving a value to a variable name or array location.  The statement:

    emp_names[0] = "kevin";

    ...use the assignment operator (=) to store a reference to the string "kevin" at position 0 in the array emp_names.  That's what "assign" means.

    If you want entries in one array to correspond to entries in another array, there's nothing in Java that will do that for you.  You must take care to put corresponding entries in corresponding positions.  That almost always means "at the same index" in each array.  So if employee #10061 is named kevin, employee 10103 is named "john", and so on, then you've already done that, too.

    When you want to process names and IDs that go together, use that common index.  For example, you could list all employees with something like:

        for (int index=0; index < emp_id.length; ++index) { // once for each index in emp_id

            if (emp_id[i] != 0) { // skip empty entries

                System.out.println(emp_id[i] + emp_names[i]);



    This strategy, putting each value you know about an entity (an employee, in this case) in a separate array, is called "parallel arrays".  That might be handy to know if you want to look for more examples in a web search.  Later, you'll find that arrays of class-type objects will usually give you more power and flexibility.

Still have questions? Get your answers by asking now.