簡體   English   中英

在 For 循環中從 LinkedList 中刪除元素; 備擇方案?

[英]Removing Elements From A LinkedList In A For Loop; Alternatives?

我正在開發一個自動從網站下載視頻的程序。 每個鏈接都有一個視頻鏈接,所以我要抓取該鏈接,然后通過重定向來實際獲取直接下載鏈接。 無論如何,我無法從 LinkedList 中刪除元素; 我正在嘗試在下載視頻后刪除該元素並將其移至下一個元素(網站 URL)。 目前,它給了我一個警告,說用iterator().remove()替換它,但是當我這樣做時,它給了我另一個錯誤:“IllegalStateException”。 我還必須知道要下載哪一集,以確定是否需要稍后在程序中跳過它(如果它已經下載)。 我已經通過設置一個字段變量來嘗試這個,但沒有太多運氣,因為我使用的是 for 循環和 for 循環使用局部變量。 我不知道還有什么要嘗試的,所以任何幫助將不勝感激! 謝謝!

package com.trentmenard;
import org.jsoup.*;
import org.jsoup.nodes.*;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.LinkedList;

import static javax.swing.WindowConstants.EXIT_ON_CLOSE;

class WebsiteScraper {
    private Document websiteConnection;
    private String episodeName;
    private String iFrameLink;
    private String URLDownloadLink;
    private String URL;
    private static DecimalFormat decimalFormat = new DecimalFormat("#.##");

    private LinkedList<String> episodeURLs = new LinkedList<>();
    private int currentEpisodeDownloading;

    WebsiteScraper(String URL) {
        for(int i = 1; i < 25; i++){
            episodeURLs.add("https://swordartonlineepisode.com/sword-art-online-season-3-episode-" + i + "-english-dubbed-watch-online/");
        }
        for(int x = 1; x < 25; x++){
            connectToURL("https://swordartonlineepisode.com/sword-art-online-season-3-episode-" + x + "-english-dubbed-watch-online/");
            episodeURLs.remove(x);
            currentEpisodeDownloading = x;
            getEpisodeName();
        }
    }

    private void connectToURL(String URL) {
        try{
            websiteConnection = Jsoup.connect(URL).get();
            System.out.println("Connection Successfully Established to: " + URL);
        }catch (IOException e)
        {e.printStackTrace();}

        if(URL.equals(iFrameLink)){
            getDownloadLink();
        }
        else if(URL.equals(URLDownloadLink)) {
            getDirectDownloadLink();
        }
    }

    private void getEpisodeName(){
        Element h2 = websiteConnection.selectFirst("h2");
        episodeName = h2.text();
        System.out.println("Found Episode Name: " + episodeName);
        getIFrameLink();
    }

    private void getIFrameLink(){
        Element iFrame = websiteConnection.selectFirst("iframe");
        iFrameLink = iFrame.attr("src");
        System.out.println("Found iFrame Link: " + iFrameLink + " for: " + episodeName);
        connectToURL(iFrameLink);
    }

    private void getDownloadLink() {
        Element hiddenID = websiteConnection.getElementById("id");
        String hiddenIDValue = hiddenID.attr("value");
        URLDownloadLink = "https://www.vidstreaming.io/download?id=" + hiddenIDValue;
        System.out.println("Found Download Link Using ID Value (" + hiddenIDValue + "): " + URLDownloadLink);
        connectToURL(URLDownloadLink);
    }

    private void getDirectDownloadLink(){
        Element downloadClass = websiteConnection.getElementsContainingOwnText("Download (orginalP - mp4)").first();
        String directDownloadLink = downloadClass.attr("href");
        System.out.println("Found Direct Download Link: " + directDownloadLink);
        downloadEpisode(directDownloadLink, episodeName);
    }

    private void downloadEpisode(String URL, String episodeName) {
        this.URL = URL;

        float Percent = 0;
        String downloadProgress = "0.00";

        JFrame progressFrame = new JFrame();
        JProgressBar progressBar = new JProgressBar(0, 100);

        progressBar.setSize(100, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);

        progressFrame.setTitle("Downloading: " + episodeName + " - " + Percent + "%");
        progressFrame.add(progressBar);
        progressFrame.setVisible(true);
        progressFrame.setLayout(new FlowLayout());
        progressFrame.setSize(575, 100);
        progressFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JLabel percentComplete = new JLabel(downloadProgress + "% complete.");
        progressFrame.add(percentComplete);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        File createFile = new File(episodeName + ".mp4");
        if (createFile.exists() && !createFile.isDirectory()) {
            System.out.println("File: " + episodeName + " Already Exists! Moving Onto Next URL.");
            // TODO: 1/26/2020 FOR STATEMENT FOR MOVING ONTO NEXT URL.
        }
        try {
            java.net.URL url = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            System.out.println("Connection Successfully Established!");
            System.out.println("Downloading File: " + episodeName);

            int filesize = connection.getContentLength();
            float totalDataRead = 0;
            byte[] data = new byte[1024];
            int i = 0;

            java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
            java.io.FileOutputStream fos = new java.io.FileOutputStream(episodeName + ".mp4");
            java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);

            while ((i = in.read(data, 0, 1024)) >= 0) {
                totalDataRead = totalDataRead + i;
                bout.write(data, 0, i);
                Percent = (totalDataRead * 100) / filesize;
                decimalFormat.setRoundingMode(RoundingMode.CEILING);
                downloadProgress = decimalFormat.format(Percent);

                progressFrame.setTitle("Downloading: " + episodeName);
                progressBar.setValue((int) Percent);
                percentComplete.setText(downloadProgress);
            }
            bout.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Connection Failed!");
        }
    }
}

Collection接口定義了一個方法removeIf(Predicate<? super T> predicate) ,顧名思義,該方法刪除滿足Predicate所有元素。 您可以使用它來避免您的IllegalStateException (雖然您沒有顯示您的堆棧跟蹤,但我想根本原因是ConcurrentModificationException )。

例如:

List<String> list = Arrays.asList("hello", "world", "abc");
list.removeIf(str -> str.length() == 5);

// list = {"abc"}

但是,在您的情況下,您正在調用list.remove(x) ,其中xList<String>上的int 由於您迭代x ,我希望這段代碼,即使您修復了IllegalStateException也會在您嘗試訪問超出范圍的索引時引發另一個異常。

iterator().remove()不起作用,因為迭代器最初沒有指向要刪除的東西。

如果要使用Iterator

Iterator<?> it = iterator();
it.next(); // Advance the iterator.
it.remove(); // Remove the previously-returned element.

但是,如果它是一個LinkedListlist.remove(0); list.removeFirst(); 會容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM