简体   繁体   English

为什么将此ArrayList视为对象列表?

[英]Why is this ArrayList being treated as a list of objects?

Let me know if I need to provide more code, but I am getting a syntax error for the last line in this snippet stating incompatible types; Object cannot be converted to String 让我知道是否需要提供更多代码,但是在此代码段的最后一行出现语法错误,指出了incompatible types; Object cannot be converted to String incompatible types; Object cannot be converted to String . incompatible types; Object cannot be converted to String I can't figure out why this ArrayList is considered to be made up of Object s instead of String s. 我不知道为什么这个ArrayList被认为是由Object而不是String的。

As you can tell, inputList was passed down from a different method, and it gets a little complicated from here to just post snippets, but as far as I can tell, everywhere else these ArrayList s are always defined as String s. 如您所知, inputList是从另一种方法传递过来的,从这里开始只发布摘要变得有些复杂,但是据我所知,在其他所有这些ArrayList始终定义为String

Any input (see what I did there?) would be appreciated! 任何输入(看看我在那做什么?)将不胜感激!

public static ArrayList<String> getURLsContainingTarget(ArrayList inputList, String target) throws MalformedURLException
{   
  ArrayList<String> outputList = new ArrayList<>();
  String regEx;
  regEx = "*" + target + "*"; 
  String line= "null";// this is a line in the web page
  // one loop to read through each URL in inputList 
  for (int i = 0; i < inputList.size() - 1; i++)
  {
    String URLline = inputList.get(i);

Your inputList is defined using a raw type - ArrayList inputList - which means, as far as the compiler is concerned, it may contain any type of Object. 您的inputList是使用原始类型ArrayList inputList ,这意味着就编译器而言,它可以包含任何类型的Object。 Therefore, inputList.get(i) returns Object and not String , and casting is required. 因此, inputList.get(i)返回Object而不是String ,并且需要强制转换。

If you change your method signature to: 如果将方法签名更改为:

public static ArrayList<String> getURLsContainingTarget(ArrayList<String> inputList, String target) throws MalformedURLException

the casting won't be required. 不需要铸造。

You can do two things: 您可以做两件事:

  1. Make inputList more strongly typed . 使inputList 类型更强。 Use ArrayList<String> inputList in the function parameter list. 在函数参数列表中使用ArrayList<String> inputList

  2. Use a cast at the point of use: String URLline = (String)inputList.get(i); 在使用点使用的流延String URLline = (String)inputList.get(i);

Be aware that (1) will cause compile time failures whereas (2) will cause runtime failures if inputList contains objects that are not String types. 请注意,如果inputList包含非String类型的对象,则(1)会导致编译时失败,而(2)会导致运行时失败。 So (1) will give you more program stability but (2) will be easier to fit into your codebase, especially if you use your function getURLsContainingTarget in many places. 因此(1)将为您提供更多的程序稳定性,但(2)将更易于装入您的代码库中,尤其是在许多地方使用函数getURLsContainingTarget情况下。

If you don't define type of ArrayList then it ll considered as Object type so you have two option : 如果您没有定义ArrayList类型,那么它将被视为对象类型,因此您有两个选择:

1 : define inputList as String like ArrayList<String> inputList 1:inputList定义为String例如ArrayList<String> inputList

2 : cast it like : String URLline = (String)inputList.get(i); 2:像这样转换: String URLline = (String)inputList.get(i);

If you don't specify a type, you're using the old non-generic version. 如果未指定类型,则说明您使用的是旧的非泛型版本。 This would basically be equivalent to: 这基本上等同于:

ArrayList<Object> inputList= new ArrayList<Object>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM