i cannot find a reliable source online that explains the proper syntax and my instructor's help is negligible. Please any insight is greatly appreciated. This is my last study guide question for my final later today! the problem is identified with arrows below. i was provided source code to a similar problem where this add seems to be working just fine that can be found below.

public Iterable<Integer> adj(int v)  {   //vertices adjacent to v
        Iterable<Integer>[] adj;
        adj = new LinkedList[V];
        adj[v] = new LinkedList<Integer>();

        for(int i = 0; i < v; i++){
            for(int j = 0; j < i; j++){
                if(matrix[i][j]){
                    adj[i].add(0, i); //<--problem here "The method add(int, int) is undefined for the type Iterable<Integer>"
                    adj[j].add(0, j); //<--problem here "The method add(int, int) is undefined for the type Iterable<Integer>"
                }
            }
        }
        return adj[v];
    }



package edu.csus.csc130.assignment4;

import java.io.File;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;


//*********** PROVIDED: *******************

/**
 * This Graph implementation maintains a vertex-indexed array of lists of integers. 
 * Every edge appears twice: if an edge connects v and w, then w appears in v’s list and v appears in w’s list. 
 */
public class Graph {
    private int V = 0; // number of vertices
    private int E; // number of edges
    private List<Integer>[] adj; // adjacency lists

    //create a V-vertex graph with no edges
    @SuppressWarnings("unchecked")
    public Graph(int V)  {
        this.V = V; 
        this.E = 0;
        adj = new LinkedList[V]; // Create array of lists.
        // Initialize all lists to empty
        for (int v = 0; v < V; v++) {
            adj[v] = new LinkedList<Integer>(); 
        }
    }

    /**
     * reads a graph from an input file, in the format V followed by E followed by a list of 
     * pairs of int values between 0 and V-1.
     */
    @SuppressWarnings("unchecked")
    public Graph(String inFileName) {
        Scanner sc = null;
        try {
            URL url = Graph.class.getResource(inFileName);
            sc = new Scanner(new File(url.getPath()));
            int numVertices = sc.nextInt();
            int numEdges = sc.nextInt();

            this.V = numVertices; 
            adj = new LinkedList[V]; // Create array of lists.
            // Initialize all lists to empty
            for (int v = 0; v < V; v++) {
                adj[v] = new LinkedList<Integer>(); 
            }

            for (int i=0; i<numEdges; i++) {
                int vertexA = sc.nextInt();
                int vertexB = sc.nextInt();
                addEdge(vertexA, vertexB);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sc != null) {
                sc.close();
            }
        }
    }

    //return number of vertices
    public int V() {
        return V;
    }

    //return number of edges
    public int E() {
        return E;
    }

    //add edge v-w to this graph
    public void addEdge(int v, int w) {
        adj[v].add(0, w);
        adj[w].add(0, v);
        E++;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(V + " vertices, " + E + " edges\n");
        for (int v = 0; v < V; v++) {
            sb.append(v + ": ");
            for (int w : this.adj(v)) {
                sb.append(w + " ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    //vertices adjacent to v
    public Iterable<Integer> adj(int v)  {
        return adj[v];
    }

    @SuppressWarnings("unused")
    public static int degree(Graph G, int v) {
        int degree = 0;
        for (int w : G.adj(v)) {
            degree++;
        }
        return degree;
    }

    public static int maxDegree(Graph G) {
        int max = 0;
        for (int v = 0; v < G.V(); v++) {
            int degree = degree(G, v);
            if (degree > max) max = degree;
        }
        return max;
    }

    public static int avgDegree(Graph G) {
        return 2 * G.E() / G.V();
    }

    public static int numberOfSelfLoops(Graph G) {
        int count = 0;
        for (int v = 0; v < G.V(); v++)
            for (int w : G.adj(v))
                if (v == w)
                    count++;
        return count / 2; 
    }   

}

Having a method and a data member with the same name adj is bound to lead to confusion.

commented: i agree, does that seem to be the problem here? i have just tried changing the list name but same issue continues. +1

Line 2: you define adj (or whatever) as an Iterable<etc>. Later you assign a LinkedList to that. That's OK because LinkedList implements Iterable. However later on you try to call add(etc), which you know is OK because you know it's called on a LinkedList. But all the compiler knows is that you call it on an Iterable, and Iterable does not include an add method.

commented: i think i am on to something, could i cast it to a linked list type? +0

You could do an (unsafe) cast, but that's not a good solution.

Can I assume the method has to return an Iterable?

Inside your method you need to construct and populate an Iterable. You have decided that a LinkedList is an appropriate data structure to do that. So declare it as a LinkedList (line 2), and then you can use all the methods that LinkedList offers. When you have finished you can simply return the LinkedList because that is a class that implements Iterable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.