So my goal for this program is to create a method that will take a string and an ArrayList
as a parameter, it will add the string to the ArrayList
and then alphabetize the whole thing and return the alphabetized ArrayList
including the string.
The line I'm having trouble with is alphabetized.get(x) = arr.get(x);
The program won't compile because it doesn't recognize the value x but I state it earlier in the for loop so I'm not really sure why it won't run...
public class TestArrays {
static Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
String pat;
ArrayList<String> names = new ArrayList<String>();
names.add("anna");
names.add("bob");
names.add("matthew");
names.add("charles");
names.add("effron");
System.out.print(newArray(names, pat));
}
public static ArrayList<String> newArray(ArrayList<String> arr, String str) {
List<String> alphabetized = new ArrayList<String>(arr.size());
arr.add(str);
java.util.Collections.sort(arr);
for (int x=0; x<=arr.size();x++){
alphabetized.get(x) = arr.get(x);
}
return alphabetized;
}
}
Thanks
A couple of things.
First, in your newArray method, there is no need to create a copy and return that copy unless, you're not wanting to alter your original ArrayList
static Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
String pat = reader.nextLine();
ArrayList<String> names = new ArrayList<String>();
names.add("anna");
names.add("bob");
names.add("matthew");
names.add("charles");
names.add("effron");
System.out.print(newArray(names, pat));
}
public static ArrayList<String> newArray(ArrayList<String> arr, String str) {
ArrayList<String> alphabetized = new ArrayList<String>(arr.size());
arr.add(str);
java.util.Collections.sort(arr);
return arr;
//for (int x=0; x<arr.size();x++){
// alphabetized.add(arr.get(x));
//}
//return alphabetized;
}
Results:
2nd, if you're going to copy to another array, understand this line of code.
ArrayList<String> alphabetized = new ArrayList<String>(arr.size());
You just created a List, that has a capacity that is the same size as the array that you passed in with no actual elements inside of it. You still have to add to the List like any other list. Once you add an element that makes the size bigger than the capacity, new memory will be automatically allocated to hold the new item.
I forgot to mention that your for loop for copying should be
for (int x = 0; x < arr.size(); x++)
Instead of
for (int x = 0; x <= arr.size(); i++)
Your original for loop will cause an IndexOutOfBounds exception, because you'll be trying to get an item from your original array that exceeds it max index value. A 10 item list will have a size of 10, but will have indexes from 0 - 9. You're original for loop will try to get index 10 which does not exist.
static Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
String pat = reader.nextLine();
ArrayList<String> names = new ArrayList<String>();
names.add("anna");
names.add("bob");
names.add("matthew");
names.add("charles");
names.add("effron");
System.out.print(newArray(names, pat));
}
public static ArrayList<String> newArray(ArrayList<String> arr, String str) {
ArrayList<String> alphabetized = new ArrayList<String>(arr.size());
arr.add(str);
java.util.Collections.sort(arr);
for (int x=0; x<arr.size();x++){
alphabetized.add(arr.get(x));
}
return alphabetized;
}
Results: