Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple List problem

Status
Not open for further replies.

bluepixie

Programmer
Jul 31, 2003
34
US
Hi, I need to iterate through the elements of a list for debugging purposes. But my IDE points out "The the type of the expression must be an array type but it resolved to List." when hovering over the obsTypes in the println statement. The same happens when I cast the observationTypes as Array or ArrayList on the 3rd line. what should I do?
Thanks!

public void setObservationTypes(List observationTypes)
{
List obsTypes = observationTypes;
for(i=0; i < obsTypes.size(); i++)
{
System.out.println(&quot;obs size: &quot; + obsTypes);
}
this.observationTypes = observationTypes;
 
PS : When posting code put it between [ code ] and [ /code ] , otherwise
Code:
[i]
gets interpreted as &quot;put in italics&quot; by the forum.

A List is not an array and can't be indexed by putting
Code:
[i]
behind it. One way to solve this is the following ;
===================
Code:
package com.ecc.tektips;

import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: ECC</p>
 * @author Ward
 * @version 1.0
 */

public class TestList {

  List observationTypes;

  public TestList() {
  }
  public static void main(String[] args) {
    TestList testList = new TestList();
    ArrayList al = new ArrayList();
    al.add(&quot;This&quot;);
    al.add(&quot;is&quot;);
    al.add(&quot;a&quot;);
    al.add(&quot;test&quot;);
    testList.setObservationTypes(al);
  }

  public void setObservationTypes(List observationTypes)
    {
        List obsTypes = observationTypes;
        for(Iterator iter = obsTypes.iterator(); iter.hasNext() ; )
        {
        System.out.println(&quot;obs size: &quot; + iter.next());
        }
        this.observationTypes = observationTypes;
    }

}
===================
 
for(i=0; i < obsTypes.size(); i++) {
System.out.println(i+&quot;) &quot; + obsTypes.get(i));
}

Sean McEligot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top