Cambiar texto de elementos de ArrayList? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

8th Apr 2018, 6:09 PM
Abraham Querido
Abraham Querido - avatar
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); } }
8th Apr 2018, 6:26 PM
Dev
Dev - avatar
+ 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
9th Apr 2018, 2:42 AM
Dev
Dev - avatar
+ 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?
9th Apr 2018, 1:33 AM
Abraham Querido
Abraham Querido - avatar
+ 1
Thanks I'll give it a try :)
9th Apr 2018, 3:13 AM
Abraham Querido
Abraham Querido - avatar