lundi 20 avril 2015

Possible incorrect calling of ArrayList index?

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...

import java.util.Scanner;
import java.util.*  ;

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

Aucun commentaire:

Enregistrer un commentaire