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:

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

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.

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

Did you read the API docs for ArrayList, yet? I'll take that as a no.

commented: Very rude individual +0

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.

commented: Very pampered student with an over blown sense of entitlement -2

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.

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. :)

Thanks everyone. Just needed a little psuedocode to gain some understanding.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.