| | |
ArrayList Elements
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
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:
Any suggestions will be greately appreciated.
Thanks,
Kimjack
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)
public void matchElements(int charA, int char) { //(psuedocode) if(list.get(charA).compareTo(list.get(charB) == 0) display charA and charB else display position of charA and charB }
Any suggestions will be greately appreciated.
Thanks,
Kimjack
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.
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
----------------------------------------------
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
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
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
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
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
----------------------------------------------
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
•
•
Join Date: Sep 2008
Posts: 1,597
Reputation:
Solved Threads: 202
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.
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.
•
•
Join Date: Sep 2009
Posts: 26
Reputation:
Solved Threads: 5
0
#7 Oct 8th, 2009
Java Syntax (Toggle Plain Text)
function(pos1, pos2) { /* counter and size */ int i, s; /* first coresponding character */ char c; s = set as array size; c = array[pos1] (as a character) if (c equals the second coresponding character) { for every i smaller than array size (s) { /* Ternary operator */ System.out.printf("%s ", (pos1 == i || pos2 == i) ? c : i + ""); } } else { for every i smaller than array size (s) { System.out.printf("%d ", i); } } }
An "optimized" code for your function may look this way.
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
----------------------------------------------
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
![]() |
Similar Threads
- read file (C#)
- Confuzzled in sorting arraylist... (Java)
- Printing out specpfic elements in arrayList (Java)
- ArrayList<MyClass>, help writing MyClass (Java)
- what is the difference between list and arraylist in c#? (C#)
- Arrays and ArrayList (Java)
- array and arrayList (Java)
Other Threads in the Java Forum
- Previous Thread: Problem with Java component layout in gui
- Next Thread: primary abstraction in java
| Thread Tools | Search this Thread |
3d @param affinetransform android api applet application arc arguments array arrays automation binary bluetooth byte capture chat class classes click client code color compare component count database design detection eclipse eclipsedevelopment encryption error event exception fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong print problem producer program programming project projectideas read recursion replaysolutions rim scanner screen server set size sms sort sql string swing terminal threads transforms tree ui web windows






