2

*Disclaimer: I am a super noob in java so bear with me.

I have a arraylist called hw_list that contains strings that were read in from a file like so:

    [Doe John 1 10 1 Introduction.java, Doe Jane 1 11 1 Introduction.java, Smith Sam 2 15 2 Introduction.java Test.java]

I was able to make each element of the array it's own sublist so it prints like so:

    [Doe John 1 10 1 Introduction.java] 
    [Doe Jane 1 11 1 Introduction.java]
    [Smith Sam 2 15 2 Introduction.java Test.java]

But to split each element into it's own sublist like above, I have to manually write each sublist out like:

    List<String> student1 = hw_list.subList(0, 1);
    List<String> student2 = hw_list.subList(1, 2);
    List<String> student3 = hw_list.subList(2, 3);

My problem is that the number of string that are read in can change so I don't know how many sublists to make ahead of time.

Is there a way to dynamically create new lists using a loop and then splitting each element based on hw_list.size()?

Is something sort of like this possible:

    for(int i=0; i<hw_list.size(); i++){
        List<String> student(i) = hw_list.sublist(i, i+1)
    }

TL;DR

How do I get a loop to create a new list for every element of an array?

8
  • 1
    Why are you splitting the element into list in the first place? I would avoid having to do this at all as it sounds overly complicated and inefficient. Commented Oct 13, 2013 at 15:14
  • @PeterLawrey Because I need to do certain things with each individual student. Commented Oct 13, 2013 at 15:17
  • 1
    Then it is better to create a Student class and do for (Student s : listOfStudents) Commented Oct 13, 2013 at 15:19
  • What do the numbers represent? The third one looks like a count of items to the right of it, but what are the first two? Commented Oct 13, 2013 at 15:22
  • 1
    Does the input file have a specific pattern, can a student have more than one name for instance? What you should do is to create a Student class and parse the lines in the file (according to a pattern) into these classes. After creating the objects you can easily work on them. Java is an object-oriented language so try to think in that paradigm while using it. Commented Oct 13, 2013 at 15:25

2 Answers 2

1

What you have coded runs fine, it does not make sense logically: single-item sublists that you have cannot be expanded by adding more elements, and they would also change with the underlying array list.

What you should do instead is building a class to represent the data stored in a single element as a group of related, meaningful items, such as first name, last name, section and date submitted, like this:

public class Student {
    private String firstName;
    private String lastName;
    private List<String> fileNames;
    private int section;
    private int date; // Consider changing this to a different type
    public Student(String firstName, String lastName, int section, int date) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.section = section;
        this.date = date;
        fileNames = new ArrayList<String>();
    }
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public int getSection() { return section; }
    public int getDateSubmitted() { return date; }
    public List<String> getFileNames() { return fileNames; }
}

Then you could make a method that takes a String, and produces a Student, like this:

private static Student studentFromString(String studentRep) {
    String[] tokens = studentRep.split(" ");
    Student res = new Student(tokens[0], tokens[1], Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]));
    // You can ignore tokens[4] because you know how many files are submitted
    // by counting the remaining tokens.
    for (int i = 5 ; i != tokens.length ; i++) {
        res.getFileNames().add(tokens[i]);
    }
    return res;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Once you have followed dasblinkenlight's advice, convert each String into a student:

List<Student> students = new ArrayList<Student>();
for(String studentRep:hw_list){
    students.add(Student.studentFromString(studentRep));
}

Then you can do stuff with your list of students, like this:

for(Student student:students){
    System.out.println(student.getFirstName());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.