Link to home
Start Free TrialLog in
Avatar of Ashok
AshokFlag for United States of America

asked on

Need to delete 1 row from this Array

String[][] phonebook = new String[max][2];

phonebook[0][0] = "Abraham";
phonebook[0][1] = "2012010222";

phonebook[1][0] = "Andy";
phonebook[1][1] = "2012010255";

phonebook[2][0] = "Bob";
phonebook[2][1] = "2012010266";

phonebook[3][0] = "Kevin";
phonebook[3][1] = "2012010277";

phonebook[4][0] = "Robert";
phonebook[4][1] = "2012010288";

using only "import java.io.*;" I need to find and entry by Name: for example "Kevin"
then delete the row if found so that new array size should be one less row than original array.

I need to do this in only one for loop.

Thanks

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can't delete from an array - you can only set to null or write a new array. Better to use a List
You can use List<String[]> or better still a List of beans
Avatar of Ashok

ASKER

how to make it null the last entry?

so if I delete 2nd row,

row1 = row1
row2 = row3
row3 = row4
row4 = row5
row5 = null

Thanks
Avatar of Ashok

ASKER

I need to do this using simple 2 dimensional array, not list or arraylist.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>
I need to find and entry by Name: for example "Kevin"
then delete the row if found so that new array size should be one less row than original array.
>>

The accepted answer doesn't do that though. If you want to do that, use the following:

http://technojeeves.com/joomla/index.php/free/66-compact-array-in-java

e.g.

String[][] compacted = CompactArray.compact(phonebook, new int[] { 3 }); // Remove 'Kevin'
	System.out.println(Arrays.deepToString(compacted));

Open in new window

Avatar of Ashok

ASKER

Before I accept the answer, there was no complete answer which I can just copy and paste to make it work.
for_yan gave me the idea on how to do it.
I coded it myself.

I need to keep the code simple, not necessarily short.
You can complain to admin of this website and have this question open again if you want.
Avatar of Ashok

ASKER

This is not for the professional work.
It is for College Project form my friend.  Because of this I need to follow strict requirements.

If it was for professional work, I would have use List.