0
Cambiar texto de elementos de ArrayList?
Hola que tal, necesito hacer un cĂłdigo que llene un ArrayList con cadenas de texto, palabras de 5 letras. y que una vez que se llena el arraylist con las palabras me lo muestre y despues me cambie todas las palabras por "*****" por cada elemento del ArrayList y que me vuelva a mostrar los elementos ya cambiados. No le se muy bien a los ArrayList. Âżalguna ayuda?
4 Answers
+ 5
// A possible solution, see if it does the job...
import java.util.ArrayList;
public class ModifyArrayList
{
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Red");
list.add("green");
list.add("blue");
list.add("orange");
list.add("yellow");
System.out.println("Original List: \n" + list);
// size() is used to get 5 (its length)
for(int i=0; i<list.size(); i++) {
String str = list.get(i);
for(int j=0; j<str.length(); j++) {
str = str.replace(str.charAt(j), '*');
}
list.remove(i);
list.add(i, str);
}
System.out.println("\nNew Modified List: \n" + list);
}
}
+ 4
Abraham Querido You must try and do it yourself! It'd be awesome if you could accomplish it yourself. If you need any help with your code, you are always welcome. :)
Let me give you a hint, check out the public methods here:
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
+ 1
Thank you very much.
Now, if I had to change the elements in pairs, for example: I have "one, two, three, four," five "in ArrayList and I want it to be:" five, four, three, two, one ".
how would it be?
+ 1
Thanks I'll give it a try :)