I want to save ArrayList from main() into my new ArrayList which is subString in geTles(ArrayList input_list_of_strings). How do I do that? I know that we need to do with the loop and add it up to subString. Also I want to break down the string word that I have in my code into small subStrings. Example: “SCHOOLWORK” will break down to “SC”,”HOO”,”LWO”,”RK”.
import java.util.ArrayList;
public class HW2 {
public static ArrayList getTiles(ArrayList input_list_of_strings) {
// create a substring by go through the loop first, then .... (instruction)
ArrayList<String> subString = new ArrayList<>();
return input_list_of_strings;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> input_words = new ArrayList<String>();
input_words.add("SCHOOLWORK");
input_words.add("BALCONY");
input_words.add("INSIST");
input_words.add("SALTPETER");
input_words.add("BOLTON");
input_words.add("KITSCHY");
input_words.add("CLIENTELE");
System.out.print(getTiles(input_words));
}
}
You’re not saving anything. You’re passing the
ArrayListinto your method by value. I’d also argue that it’s pretty important that you design to the interface instead of the concrete implementation (that is, you’d be usingListinstead), and you type it so that the behavior you need in your method doesn’t involve casts or temporary variables.Here’s what I mean.