我在理解如何正确添加到数组时遇到一些麻烦。我想要完成的是将方法的结果添加到数组中,然后运行数组。
这是我需要的一个例子,或者假设我需要:
数组{ “websiteaddress”, “websiteaddress”, “websiteaddress”, “websiteaddress”, “websiteaddress”}
但我得到了:
websiteaddress
websiteaddress
websiteaddress
websiteaddress
websiteaddress
websiteaddress
以下是我正在使用的代码:
private static final String webSiteURL = "https://websitename.com/";
//The path of the folder that you want to save the images to
private static final String folderPath = "C://path/";
private static final ArrayList<String> webPages = new ArrayList<>();
public static String[] thisIsAStringArray = {"https://websitename.com/"};
public static String[] tempArray = new String[ thisIsAStringArray.length + 1 ];
/**
*
* Method description:
* Date: Mar 17, 2018
* @param args
* @return void
*/
public static void main(String[] args) {
String path = folderPath + getPageTitle(webSiteURL).replaceAll(" ", "-");
pageLinks(webSiteURL);
System.out.println(thisIsAStringArray);
for(String web : thisIsAStringArray)
{
for(int n = 0; n < thisIsAStringArray.length - 1; n++)
{
System.out.println(thisIsAStringArray[n]);
getPageTitle(web);
pageLinks(web);
creatDirectory(folderPath, getPageTitle(web));
getsImagesAndSaves(path, web);
n++;
}
}
}
/**
*
* Method description: Get all the links on the page and put them into an array
* Date: Mar 16, 2018
* @param src
* @return void
*/
public static void pageLinks(String src)
{
try
{
URL url = new URL(src);
Document doc = Jsoup.parse(url, 3*1000);
Elements links = doc.select("a[href]"); // a with href
for (Element link : links)
{
System.out.println(link.attr("abs:href"));
String noHref = link.attr("abs:href");
for(int i = 0; i < thisIsAStringArray.length; i++)
{
tempArray[i] = thisIsAStringArray[i];
}
//thisIsAStringArray[i] = noHref;
tempArray[thisIsAStringArray.length] = noHref;
}
thisIsAStringArray = tempArray;
}
catch(Exception error)
{
System.out.println(error + " Something went wrong getting the links!");
}
}
}
任何帮助将不胜感激,并提前感谢您!
答案 0 :(得分:0)
您有2个阵列:thisIsAStringArray
,大小为1,tempArray
大小为2.他们的大小是固定的,无法更改!现在你有一个循环:
for (Element link : links)
{
...
for(int i = 0; i < thisIsAStringArray.length; i++)
{
tempArray[i] = thisIsAStringArray[i];
}
}
读取 - 对于您找到的每个链接,i
从0循环到1(这意味着内部循环i
内部只有值0)并且将链接添加到第一个位置(索引为0)
您无法在运行时更改数组的大小。如果您无法预先知道您将拥有多少项,则必须使用List
。尝试这样的事情:
ArrayList<String> myList = new ArrayList<>();
for (Element link : links)
myList.add(link);