arrays - Remove values of an entire row java -
i have string array has 10 rows , 4 columns holds contact information. if user wants remove contact, maintain 10 rows , 4 columns - without using lists in fashion - how done? instance:
contacts[0][0] = fname; //charlie contacts[0][1] = lname; //brown contacts[0][2] = pnum; //(309)555-1212 contacts[0][3] = age1; //10 contacts[1][0] = fname1; //sally contacts[1][1] = lname; //brown contacts[1][2] = pnum; //(309)555-1212 contacts[1][3] = age3; //8
now user wants remove charlie brown contacts, , move other rows fill in empty space, maintaining total of 10 rows , 4 columns.
the lastest attempt is:
private void removecontact(string[][] contacts) { scanner input = new scanner(system.in); string lname; string fname; int findex; int lindex; system.out.println("what first name of contact remove?"); fname = input.nextline(); system.out.println("what last name of contact remove?"); lname = input.nextline(); boolean found = false; (int = 0; < contacts.length; i++) { (int j = 0; j < 4; j++) { if (contacts[i][0].equalsignorecase(fname) && contacts[i][1].equalsignorecase(lname)) { found = true; break; } else { system.out.println("that contact not found."); return; } } if (found) { contacts[i][0] = null; contacts[i][1] = null; contacts[i][2] = null; contacts[i][3] = null; return; } } }
this works great if want remove charlie brown, other row...it doesn't work
you have code for-loop shift elements if want use arrays.
to achieve this:
int ind; //the index you'd remove. for(int = ind + 1; < array.length; i++){ array[i - 1] = array[i]; } //set last value nulls or something.
consider arraylist: https://docs.oracle.com/javase/7/docs/api/java/util/arraylist.html it'll shifting you.
also, might want make people
objects , make array (or arraylist) 1 of people
. way won't have keep track of attributes of contacts arbitrary numbers , can have more readable , re-usable code.
Comments
Post a Comment