List to Array -Method (W06)

    0

    0

    anlinn

    The listToArray()method takes a List variable as an argument and returns an int array. The List variable is set to this, and the length of the List is calculated to be n. The method then loops through the List and sets the value of i to 0, 1, 2, etc until the value of n is reached. The value of i is then used to access the value of t.info in the List, and t is set to the next Element in the List.

    public int[] listToArray(){
      List t =this;
      int n= length();
      int[] a = new int[n];
      for (int i=0; i<n; ++1){
        a[i] = t.info;
        t= t.next;
      
      
      }
      return a;
    
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.