954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Printing out specpfic elements in arrayList

HI all wonder if someone can help, i want to be able to printout specific elements within an arrayList without using array keys. I have the following code, which will print out every element in the array. I want to print out every 2nd element. So so i had a array containing 1,2,3,4,5,6,7,8,9,10, what i want to be able to do is print out every 2nd element, so end up with 2,4,6,8,10. Any ideas?

for( int i = 0; i < words.length; i++ ){
			System.out.println( words[ i ] );
		}
VinceAshbySmith
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I'm confused what you mean by array keys. You also say you want to print specific elements in an ArrayList, then change and say Array. In any case, with the code you have provided, just change i++ to i+=2 That will print out every 2nd element.

jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You