944,173 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1117
  • Java RSS
Oct 8th, 2009
0

ArrayList Elements

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 8th, 2009
0
Re: ArrayList Elements
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 8th, 2009
0
Re: ArrayList Elements
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
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 8th, 2009
0
Re: ArrayList Elements
Did you read the API docs for ArrayList, yet? I'll take that as a no.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 8th, 2009
-2
Re: ArrayList Elements
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.
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 8th, 2009
1
Re: ArrayList Elements
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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 8th, 2009
0
Re: ArrayList Elements
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 19
Solved Threads: 6
Light Poster
nomemory is offline Offline
31 posts
since Sep 2009
Oct 8th, 2009
0
Re: ArrayList Elements
Thanks everyone. Just needed a little psuedocode to gain some understanding.
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 9th, 2009
0
Re: ArrayList Elements
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem with Java component layout in gui
Next Thread in Java Forum Timeline: primary abstraction in java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC