Results 1 to 10 of 10
Thread: Array manipulation
- 07-17-2008, 12:47 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
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..
ouput:Java 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]); } } }
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
- 07-17-2008, 01:11 AM #2
Keyed data storage.
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-17-2008, 01:27 AM #3
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:
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 beJava Code:1573187[COLOR="White"]_[/COLOR]CEO id 2871882[COLOR="White"]_[/COLOR]worker id 36963[COLOR="White"]___[/COLOR]worker id
Here is the solution:Java Code:arr[row][column]
which gives the outputJava 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(); } }
I hope this was what you where looking for, Ms.Ranjana. ;)Java Code:worker id 2871882 36963 027634 964956
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 07-17-2008, 01:48 AM #4
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
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
- 07-17-2008, 03:38 AM #5
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;
With the above println() statement there is no way to get:System.out.println(arr[i][1]+" "+arr[i][0]+" "+arr[j][0]);
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.worker id 2871882 36963 027634 964956
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 03:48 AM.
- 07-17-2008, 11:09 AM #6
Method added
Hello Ms.Ranjan
Here is the revision:
The code in blue is important. The program outputs:Java 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]="Man[COLOR="RoyalBlue"]a[/COLOR]ger id"; arr[7][0]="83736"; arr[7][1]="CEO id"; printIDs("CEO id"); printIDs("worker id"); printIDs("Manager id"); } [COLOR="RoyalBlue"] 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(); }[/COLOR] }
Does this help you, Ms.Ranjan? ;)Java Code:CEO id 1573187 964956 83736 worker id 2871882 36963 027634 Manager id 190444 38914
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 07-17-2008, 12:20 PM #7
Member
- Join Date
- Jun 2008
- Posts
- 85
- Rep Power
- 0
Thankyou very much Tim,Norm and Nicholas i could solve it...
once again thanks..
- 07-17-2008, 10:39 PM #8
It's my pleasure Ms.Ranjan ;)
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
- 07-17-2008, 11:53 PM #9
Hey, this is fun. This is multi-dimentional array.
- 07-18-2008, 09:10 PM #10
Hello matt_well. I agree with you. You can make a datastructure with any number of dimensions. It only gets more complicated to visualize.
This is how I picture them. One can go on forever. ;)Java Code:1 dimension[COLOR="White"]__[/COLOR]- 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
Eyes dwelling into the past are blind to what lies in the future. Step carefully.
Similar Threads
-
How do I strip down integers for future manipulation?
By frasifrasi in forum New To JavaReplies: 12Last Post: 06-14-2008, 02:17 AM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM -
[SOLVED] File Movement/Manipulation
By Leprechaun in forum New To JavaReplies: 2Last Post: 04-23-2008, 12:39 AM -
String manipulation example (Title case)
By Java Tip in forum Java TipReplies: 0Last Post: 01-29-2008, 09:04 AM -
String Manipulation Task
By hiranya in forum New To JavaReplies: 1Last Post: 11-19-2007, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks