0

I'm having trouble getting the arraylist length to show in my for loop. I'm a beginner/novice with programming so I might be completely off base here. The output for the ArrayList is showing up as 0. What I'm trying to do it compare a string from a jTextBox with an arraylist and see if the String exists within the List.

public static void ValidateProtocolCompatiblity(FAS_Define[] faslist, String selectedProtocol, String selectedFAS){
     for (int index=0; index < faslist.length;index++){
        if (faslist[index].getName() == selectedFAS)
            for (int j=0; j < faslist[index].getSupportedProtocol().toArray().length;j++){       
                fasProtocolList.add(faslist[index].getSupportedProtocol().toString());

            }
    }

Here is my original array list based on the constructor

        FAS_Define[] fasList = new FAS_Define[10];
        fasList[0] = new FAS_Define("FAS2552", new ArrayList(Arrays.asList("iSCSI", "FCoE"))); 

        ValidateProtocolCompatiblity(fasList,protocolField, fasField);

Class for the getter/setter/constructor outlining the original array list

public class FAS_Define {
private int id;
private String name;
private List<String> supportedProtocol; 

private static int numberofDevice = 0;

public FAS_Define(String cName, ArrayList<String> cSupportedProtocol){
    this.name = cName;
    this.supportedProtocol = new ArrayList<String>();

    numberofDevice++;
    id = numberofDevice;
}
public String getName(){return name;}
public void setName(String name){this.name = name;}

public List<String> getSupportedProtocol(){return supportedProtocol;}
public void setSupportedProtocol(List<String> supportedProtocol){this.supportedProtocol = supportedProtocol;}
Matthias
  • 4,481
  • 12
  • 45
  • 84
Karl
  • 17
  • 1
  • 7
  • 4
    `if (faslist[index].getName() == selectedFAS)` don't compare strings with `==`. Use `equals` method. More info at "[How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)" – Pshemo Mar 16 '16 at 19:30
  • OP has plenty of bugs that could benefit from reopening. – shmosel Mar 16 '16 at 19:40

1 Answers1

0

Instead of

faslist[index].getSupportedProtocol().toArray().length

Use:

faslist[index].getSupportedProtocol().size()

There's no need to convert the arrayList to array. And for getting the element of the array list at a given index, use:

faslist.get(index).getSupportedProtocol().toString()

Instead of

faslist[index].getSupportedProtocol().toString()
Vucko
  • 7,371
  • 2
  • 27
  • 45