• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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

0
Ashok
Asked:
Ashok
  • 4
  • 3
  • 2
1 Solution
 
CEHJCommented:
You can't delete from an array - you can only set to null or write a new array. Better to use a List
0
 
CEHJCommented:
You can use List<String[]> or better still a List of beans
0
 
AshokAuthor Commented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AshokAuthor Commented:
I need to do this using simple 2 dimensional array, not list or arraylist.
0
 
for_yanCommented:
You just first assign last row values to rthe inndexes where you want to remove and the assign nulls to the last row elemmnts
0
 
CEHJCommented:
>>
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

0
 
AshokAuthor Commented:
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.
0
 
AshokAuthor Commented:
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now