So here is my methods:
public static ArrayList collectSchedule(ArrayList schedule, ArrayList list, int counter)
{
((ArrayList) list.get(counter)).add(schedule);
System.out.println(list);
return list;
}
public static ArrayList getClassInfo(int count)
{
Scanner stdIn = new Scanner (System.in);
String userInput;
System.out.print("Enter the name of class #" + count + " : ");
userInput = stdIn.nextLine();
ArrayList x = getClassTime();
x.add(0, userInput);
x.add(1, getClassLength());
x.add(2, getDayOfTheWeek());
return x;
}
So I take the value “x” from getClassInfo (which is an ArrayList) and input that into the method collectSchedule along with a number (the index of where I want it) and another ArrayList (lets call it “schedule”). I add “x” (the ArrayList) to the ArrayList “schedule”. If I print out “x” it will show all values of “schedule”. (note: I add multiple different instances of “x” to schedule!). So if I add 3 different “x’s” to “schedule”, it will print them all out separately ( [x, x, x][x, x, x][x, x, x] ). I’m having troubles getting only a single value of “x” from “schedule”.
I thought this code would work but it gives me an error at compilation.
public static void creation(ArrayList list)
{
System.out.println((schedule.get(0)).x().get(1));
}
Can anyone help me where I’ve gone wrong?
I read something about using an object? But not sure how!
Thanks!
You’ll find working with collections a lot easier to do if you start using generics, i.e.
List<List<String>> myList. Additionally, why not make some actual classes that store typed values instead of relying on indexed lists?That aside….
If the type of schedule is ArrayList, then schedule.get(0) is going to return something of type Object, unless you perform an explicit cast. Since Object has no method x() that’s going to generate an error. You can typically narrow this stuff down yourself by not including so many chained function calls on one line. Doing so makes it difficult to tell what a given error is referring to.
If schedule were declared as
List<List<String>>or evenList<List<Object>>then you’d be able to do this:Which I think is what you’re actually aiming for