So we have the following:
ArrayList<String> list = ( new ArrayList<String>( ) );
list.add( new String( "hello" ) ); // add first word at position [0]
list.add( new String( "world" ) ); // add second word at position [1]
I do not want to use the .replace method as that will replace ALL the occurrences. Suppose I only want to modify the first "l" in the hello word and change it to "x", how would I do that? I only want to target a specific element # within the string within the array.
System.out.println( list ); // display the full arraylist
Initial Output:
[hello, world]
Desired Output:
[hexlo, world]
new String()for a string literal. You're creating an extra object for nothing.replaceFirst()method or use aStringBuilderinstead of aStringand use thesetCharAt()method.new String(…)creation; you don't need to putnew ArrayList<String>()in braces.