Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have an array list, i want to iterate through the arraylist and assign each element in the arraylist the value "hell". How do i that?


/**
 * 
 * 
 */
package practicebook;

import java.util.*; 

public class arraylist1 {

	public static void main(String[] args) {
	// TODO Auto-generated method stub
		
		
		ArrayList <String> myString = new ArrayList<String>(); 
		
	
		System.out.println(myString.size());
		
		myString.add("Eve");
		myString.add("Anna");
		myString.add("Tonny");
		myString.add("Steve");
		
		
		
		Iterator<String> myStringIterator =  myString.iterator();
		
		
		for(int i=0; i< myString.size() ;++i)
		{
			
			String a = myString.get(i) ; 
			
			myString.get(i) = "hell" ;
		
		}
		
		 
	}
	
}
//end main}
// end class


What I have tried:

*********************************************************************************************************************************************************************************
Posted
Updated 15-Feb-17 15:45pm
Comments
[no name] 15-Feb-17 16:05pm    
This is so discouraging. Do you know the difference between "get" and "set"? Trying to learn programming one not-a-question at a time is just not working for you. Maybe you should try something else?

1 solution

try and adapt from this example:
Iterator<String> myStringIterator = myString.iterator();
while (myStringIterator.hasNext()) {
	System.out.println(myStringIterator.next());
}
 
Share this answer
 
v4
Comments
forte74 27-Feb-17 19:42pm    
when i use debug mode for this while loop, Im getting :
Exception in thread "main" java.util.NoSuchElementException
at java.util.ArrayList$Itr.next(Unknown Source)
at practicebook.arraylist1.main(arraylist1.java:50)
Peter Leow 27-Feb-17 22:32pm    
I have no access to your computer. Something else is not right. Anyway, check this out to convince yourself http://rextester.com/UQI80985

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900