K
K
Kostya Bakay2016-02-03 04:54:06
Java
Kostya Bakay, 2016-02-03 04:54:06

How to add an instance of my class to ArrayList via For-Each?

I couldn't figure out how to properly formulate the question in the title, but I'll try to explain what I want to do. I have a method that parses posts on a website page and a Story class that has one post per page. The post consists of many components and therefore I do a separate parsing of each of them - so far it's just the post id and its content.
For HTML posts, the tags are repeated, so the story variable will store all 10 posts on the page. The same goes for the id variable. This code is working and outputs the content of each post as an element of an ArrayList. I want to display the post id and its content in one ArrayList element. In the comments of the method, I showed how I want to do it.

public class Story {
    private String story;
    private String storyId;

    public Story(String story) {
        this.story = story;
    }

    public Story(String storyId, String story) {
        this.storyId = storyId;
        this.story = story;
    }

    @Override
    public String toString() {
        return story;
    }

//    @Override
//    public String toString() {
//        return storyId + "\n" + story;
//    }
}

public void parseDocument(Document doc) {

            // Парсит посты на странице

            // Парсит контент на странице
            story = doc.select("[style=margin:0.5em 0;line-height:1.785em]");

            // Парсит id на странице
            id = doc.getElementsByClass("col-xs-6");

            ArrayList<Story> storyList = new ArrayList<Story>();

            for (Element stories : story) {
                    //storyList.add(new Story(id.text(), story.text()));
                    // Думал сделать так, но тогда все посты на странице заносятся в один 
                    // элемент ArrayList и тем более id вообще не обрабатываются из-за условий цикла.
                    // Хотелось бы сделать условие цикла как-то так:
                    // for (Element stories : story && Element ids : id) 

                    // Рабочий вариант, но работает только с переменной story - контентом поста
                    storyList.add(new Story(stories.text()));
            }

            for (Story stories: storyList) {
                titleList.add(stories);
            }
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Dorofeev, 2016-02-04
@kostyabakay

Try to go via Iterator:

Iterator<Element> idIterator = ids.iterator();
Iterator<Element> storyIterator = stories.iterator();

while ( idIterator.hasNext() && storyIterator.hasNext()) {
    storyList.add(new Story(idIterator.next().text(), storyIterator.next().text());
}

The advantage of this method is that you can change the implementation of the collection at any time
without changing this code.

A
Alexey Reznichenko, 2016-02-03
@ReznichenkoLex

Isn't Elements an inheritor of ArrayList?
If yes, then you can:

for (int i = 0; i < story.size(); i++)
            storyList.add(new Story(id.get(i).text(), story.get(i).text());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question