Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-17-2008, 01:47 AM
Member
 
Join Date: Jun 2008
Posts: 76
Rep Power: 0
Ms.Ranjan is on a distinguished road
Default Array manipulation
Hi

i have problem here is the code for it.i have to display the numbers associated with worker id in the following format given below..

Code:
String arr[][]=new String[6][2];

arr[0][0]="1573187";
arr[0][1]="CEO id";
arr[1][0]="2871882";
arr[1][1]="worker id";
arr[2][0]="36963";
arr[2][1]="worker id";
arr[3][0]="190444";
arr[3][1]="Manager id";
arr[4][0]="027634";
arr[4][1]="worker id";
arr[5][0]="964956";
arr[5][1]="worker id";


for(int i=0;i<6;i++)
{
   for(int j=i+1;j<6;j++)
     {
        if(arr[i][1].equals(arr[j][1]))

          {
             System.out.println(arr[i][1]+" "+arr[i][0]+" "+arr[j][0]);
            }
       }
  }
ouput:
worker id 2871882 36963
worker id 2871882 027634
worker id 2871882 964956
worker id 36963 027634
worker id 36963 964956
worker id 027634 964956

But
Expected Output is
worker id 2871882 36963 027634 964956

can anyone please help
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-17-2008, 02:11 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Default Keyed data storage.
Originally Posted by Ms.Ranjan View Post
....can anyone please help
Yes, use a Map of Worker objects keyed by an id field. I enjoy digging through code, but this array indexing is tedious and error prone.
__________________
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-17-2008, 02:27 AM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 410
Rep Power: 3
tim is on a distinguished road
Default A search in a table
Hello Ms.Ranjana

Here is a solution for your problem. A simple way to think about two-dimensional arrays in Java is "there is no two-dimensional array". Yes, it does sound corny, but it's true. Java does not support two-dimensional arrays like Pascal for example. In Java, we have an array of arrays. So in your problem, consider the array to be an array of Strings. So the first index will specify which array you are looking at and the second index will get the element in the current array at the specified index. Consider the table below:
Code:
1573187_CEO id
2871882_worker id
36963___worker id
where each row represents an array and each column represents a part of the array. Notice that the second column gives the type of ID and the first gives the value. So the indexes would be
Code:
arr[row][column]
Here is the solution:
Code:
public class Main{
	public static void main(String[] arg) {
		String arr[][]=new String[6][2];

		arr[0][0]="1573187";
		arr[0][1]="CEO id";
		arr[1][0]="2871882";
		arr[1][1]="worker id";
		arr[2][0]="36963";
		arr[2][1]="worker id";
		arr[3][0]="190444";
		arr[3][1]="Manager id";
		arr[4][0]="027634";
		arr[4][1]="worker id";
		arr[5][0]="964956";
		arr[5][1]="worker id";


		String search = "worker id";
		System.out.print(search);
		for(int i=0;i<6;i++){
			if (arr[i][1].equals(search)){
				System.out.print(" " + arr[i][0]);
			}
		}
		System.out.println();
	}
}
which gives the output
Code:
worker id 2871882 36963 027634 964956
I hope this was what you where looking for, Ms.Ranjana.
__________________
Eyes dwelling into the past are blind to what lies in the future.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-17-2008, 02:48 AM
Member
 
Join Date: Jun 2008
Posts: 76
Rep Power: 0
Ms.Ranjan is on a distinguished road
Default
thankyou....but the problem is i have lots of records like this so i cannot search for worker id alone and display the number related to it

arr[0][0]="1573187";
arr[0][1]="CEO id";
arr[1][0]="2871882";
arr[1][1]="worker id";
arr[2][0]="36963";
arr[2][1]="worker id";
arr[3][0]="190444";
arr[3][1]="Manager id";
arr[4][0]="027634";
arr[4][1]="worker id";
arr[5][0]="964956";
arr[5][1]="CEO id";
arr[6][0]="38914";
arr[6][1]="Manger id";
arr[7][0]="83736";
arr[7][1]="CEO id";

output should be:
CEO id 1573187 964956 83736
worker id 2871882 36963 027634
Manager id 190444 38914
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-17-2008, 04:38 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
A suggestion on making your code more readable: use a final variable instead of a literal for the indexes.
Instead of arr[i][0] use arr[i][WrkrNbrIdx] & arr[i][WrkrNameIdx]
with these definitions;
final int WrkrNbrIdx = 0;
final int WrkrNameIdx = 1;

Quote:
System.out.println(arr[i][1]+" "+arr[i][0]+" "+arr[j][0]);
With the above println() statement there is no way to get:
Quote:
worker id 2871882 36963 027634 964956
I think you need to state what the assignment is more clearly. Looking at you second post, it looks like you want to find all the different worker ids and for each one, output the numbers associated with it.
For this you'll need a way to remember all the unique worker ids, say a Map as Nicholas suggests, using the worker id as the key and storing the numbers as the value associated with the key. So you'll need to think of an class that can store the numbers.

Last edited by Norm; 07-17-2008 at 04:48 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-17-2008, 12:09 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 410
Rep Power: 3
tim is on a distinguished road
Default Method added
Hello Ms.Ranjan

Here is the revision:
Code:
public class Main{
	private static String arr[][]=new String[8][2];

	public static void main(String[] arg) {
		arr[0][0]="1573187";
		arr[0][1]="CEO id";
		arr[1][0]="2871882";
		arr[1][1]="worker id";
		arr[2][0]="36963";
		arr[2][1]="worker id";
		arr[3][0]="190444";
		arr[3][1]="Manager id";
		arr[4][0]="027634";
		arr[4][1]="worker id";
		arr[5][0]="964956";
		arr[5][1]="CEO id";
		arr[6][0]="38914";
		arr[6][1]="Manager id";
		arr[7][0]="83736";
		arr[7][1]="CEO id";
		printIDs("CEO id");
		printIDs("worker id");
		printIDs("Manager id");
	}

	public static void printIDs(String key){
		System.out.print(key);
		for(int i = 0; i < arr.length; i++){
			if (arr[i][1].equals(key)){
				System.out.print(" " + arr[i][0]);
			}
		}
		System.out.println();
	}
}
The code in blue is important. The program outputs:
Code:
CEO id 1573187 964956 83736
worker id 2871882 36963 027634
Manager id 190444 38914
Does this help you, Ms.Ranjan?
__________________
Eyes dwelling into the past are blind to what lies in the future.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-17-2008, 01:20 PM
Member
 
Join Date: Jun 2008
Posts: 76
Rep Power: 0
Ms.Ranjan is on a distinguished road
Smile
Thankyou very much Tim,Norm and Nicholas i could solve it...

once again thanks..
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-17-2008, 11:39 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 410
Rep Power: 3
tim is on a distinguished road
Default
It's my pleasure Ms.Ranjan
__________________
Eyes dwelling into the past are blind to what lies in the future.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-18-2008, 12:53 AM
matt_well's Avatar
Member
 
Join Date: Jul 2008
Posts: 59
Rep Power: 0
matt_well is on a distinguished road
Default
Hey, this is fun. This is multi-dimentional array.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-18-2008, 10:10 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 410
Rep Power: 3
tim is on a distinguished road
Default
Originally Posted by matt_well View Post
Hey, this is fun. This is multi-dimentional array.
Hello matt_well. I agree with you. You can make a datastructure with any number of dimensions. It only gets more complicated to visualize.
Code:
1 dimension__- a line
2 dimensions - a block
3 dimensions - a cube
4 dimensions - a animated cube
5 dimensions - a line of animated cubes
6 dimensions - a block of animated cubes
7 dimensions - a cube of animated cubes
8 dimensions - a animated cube of animated cubes
This is how I picture them. One can go on forever.
__________________
Eyes dwelling into the past are blind to what lies in the future.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I strip down integers for future manipulation? frasifrasi New To Java 12 06-14-2008 03:17 AM
Array Reflection: Multi Array Reflection Java Tip java.lang 0 04-23-2008 09:08 PM
[SOLVED] File Movement/Manipulation Leprechaun New To Java 2 04-23-2008 01:39 AM
String manipulation example (Title case) Java Tip Java Tips 0 01-29-2008 10:04 AM
String Manipulation Task hiranya New To Java 1 11-19-2007 12:07 PM


All times are GMT +2. The time now is 05:12 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org