ArrayList Elements

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

ArrayList Elements

 
0
  #1
Oct 8th, 2009
Hi all,

I wondering how to get started with this. I am working on a method that will take two ints. These ints represent positions in an arraylist of characters. I am trying to determine if the elements of the two positions are equal. If, so display the elements, if not display the int position. For example f the arraylist is as follows:

ArrayList<Character> list = new ArrayList<Character>();
list.add('a');
list.add('f');
list.add('c');
list.add('a');
list.add('s');

and the method is:

  1. public void matchElements(int charA, int char)
  2. {
  3. //(psuedocode)
  4. if(list.get(charA).compareTo(list.get(charB) == 0)
  5. display charA and charB
  6. else
  7. display position of charA and charB
  8. }

Any suggestions will be greately appreciated.

Thanks,
Kimjack
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,394
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
Oct 8th, 2009
Okay, and your question is?

If you read the API docs for ArrayList you will find a method for retreiving an Object at a specific index. Use it.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster
 
0
  #3
Oct 8th, 2009
I understand how to get elements from an array. I am working on the display. How to display elements of an array at certain positions. If charA and charB are matches then display the positions of the array along with the correctly matched elements as such:

1, 2, 3, 4, T, 6, 7, T, 9

if charA and charB do not match display:
1, 2, 3, 4, 5, 6, 7, 8, 9

Thanks in advance for any suggestions from all.

Kimjack
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,394
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #4
Oct 8th, 2009
Did you read the API docs for ArrayList, yet? I'll take that as a no.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster
 
-2
  #5
Oct 8th, 2009
I have read it many times, if I understand all I would not be asking for assistance. If you cannot provide assitance without being rude, I would rather you not respond at all.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 201
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
1
  #6
Oct 8th, 2009
It seems like you already figured out how to get the Characters from the ArrayList and compare them to see if they are the same. Now all you need to do is write a for loop that prints from 1 . . n where n is the number of elements in your ArrayList. But once you get to the index where the Characters are that you were supposed to compare, you need an if statement to decide whether to print out the Character itself or whether to print out the index.

P.S. Masijade wasn't being rude, just straightforward. You seem to have the basic idea down, so if you know how to use a for loop and an if statement, you should be able to do this.
Out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 26
Reputation: nomemory is an unknown quantity at this point 
Solved Threads: 5
nomemory nomemory is offline Offline
Light Poster
 
0
  #7
Oct 8th, 2009
  1. function(pos1, pos2) {
  2. /* counter and size */
  3. int i, s;
  4.  
  5. /* first coresponding character */
  6. char c;
  7.  
  8. s = set as array size;
  9. c = array[pos1] (as a character)
  10.  
  11. if (c equals the second coresponding character) {
  12. for every i smaller than array size (s) {
  13. /* Ternary operator */
  14. System.out.printf("%s ", (pos1 == i || pos2 == i) ? c : i + "");
  15. }
  16. } else {
  17. for every i smaller than array size (s) {
  18. System.out.printf("%d ", i);
  19. }
  20. }
  21. }

An "optimized" code for your function may look this way.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster
 
0
  #8
Oct 8th, 2009
Thanks everyone. Just needed a little psuedocode to gain some understanding.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,394
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #9
Oct 9th, 2009
To gain some understanding of what? You essentially asked for a complete "user's guide" to your assignment. If this is your assignment, I can guarantee that other assignments before this have used the print/println statement, so the "question" about "display" is a moot point (unless you need to show it in a dialog, but then you should say that, if that is the case). Now, you might, and I stress, might, not have had anything to do with loops yet, but I doubt that, so I must ask how much sleep you are getting at night to need this much sleep during your classes?
Last edited by masijade; Oct 9th, 2009 at 3:19 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC