Note that there are some explanatory texts on larger screens.

plurals
  1. POException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at AdjMatrixDigraph.main(AdjMatrixDigraph.java:90)
    text
    copied!<p>In my code I am trying to derive an adjacency matrix, but I'm getting the following exception.</p> <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at AdjMatrixDigraph.main(AdjMatrixDigraph.java:90) </code></pre> <p>AdjMatrixDigraph.java</p> <pre><code>import java.util.Iterator; import java.util.NoSuchElementException; public class AdjMatrixDigraph extends StdOut { private int V; private int E; private boolean[][] adj; // empty graph with V vertices public AdjMatrixDigraph(int V) { if (V &lt; 0) throw new RuntimeException("Number of vertices must be nonnegative"); this.V = V; this.E = 0; this.adj = new boolean[V][V]; } // random graph with V vertices and E edges public AdjMatrixDigraph(int V, int E) { this(V); if (E &lt; 0) throw new RuntimeException("Number of edges must be nonnegative"); if (E &gt; V*V) throw new RuntimeException("Too many edges"); // can be inefficient while (this.E != E) { int v = (int) (V * Math.random()); int w = (int) (V * Math.random()); addEdge(v, w); } } // number of vertices and edges public int V() { return V; } public int E() { return E; } // add directed edge v-&gt;w public void addEdge(int v, int w) { if (!adj[v][w]) E++; adj[v][w] = true; } // return list of neighbors of v public Iterable&lt;Integer&gt; adj(int v) { return new AdjIterator(v); } // support iteration over graph vertices private class AdjIterator implements Iterator&lt;Integer&gt;, Iterable&lt;Integer&gt; { private int v, w = 0; AdjIterator(int v) { this.v = v; } public Iterator&lt;Integer&gt; iterator() { return this; } public boolean hasNext() { while (w &lt; V) { if (adj[v][w]) return true; w++; } return false; } public Integer next() { if (hasNext()) { return w++; } else { throw new NoSuchElementException(); } } public void remove() { throw new UnsupportedOperationException(); } } // string representation of Graph - takes quadratic time public String toString() { String NEWLINE = System.getProperty("line.separator"); StringBuilder s = new StringBuilder(); s.append(V + " " + E + NEWLINE); for (int v = 0; v &lt; V; v++) { s.append(v + ": "); for (int w : adj(v)) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } // test client public static void main(String[] args) { int V = Integer.parseInt(args[3]); int E = Integer.parseInt(args[1]); AdjMatrixDigraph G = new AdjMatrixDigraph(V, E); StdOut.println(G); } } </code></pre> <p>StdOut.java</p> <pre><code>import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.Locale; /** * &lt;i&gt;Standard output&lt;/i&gt;. This class provides methods for writing strings * and numbers to standard output. * &lt;p&gt; * For additional documentation, see &lt;a href="http://introcs.cs.princeton.edu/15inout"&gt;Section 1.5&lt;/a&gt; of * &lt;i&gt;Introduction to Programming in Java: An Interdisciplinary Approach&lt;/i&gt; by Robert Sedgewick and Kevin Wayne. */ public class StdOut { // force Unicode UTF-8 encoding; otherwise it's system dependent public static final String charsetName = "UTF-8"; // assume language = English, country = US for consistency with StdIn private static final Locale US_LOCALE = new Locale("en", "US"); // send output here private static PrintWriter out; // this is called before invoking any methods static { try { out = new PrintWriter(new OutputStreamWriter(System.out, charsetName), true); } catch (UnsupportedEncodingException e) { System.out.println(e); } } // don't instantiate public StdOut() { } // close the output stream (not required) /** * Close standard output. */ public static void close() { out.close(); } /** * Terminate the current line by printing the line separator string. */ public static void println() { out.println(); } /** * Print an object to standard output and then terminate the line. */ public static void println(Object x) { out.println(x); } /** * Print a boolean to standard output and then terminate the line. */ public static void println(boolean x) { out.println(x); } /** * Print a char to standard output and then terminate the line. */ public static void println(char x) { out.println(x); } /** * Print a double to standard output and then terminate the line. */ public static void println(double x) { out.println(x); } /** * Print a float to standard output and then terminate the line. */ public static void println(float x) { out.println(x); } /** * Print an int to standard output and then terminate the line. */ public static void println(int x) { out.println(x); } /** * Print a long to standard output and then terminate the line. */ public static void println(long x) { out.println(x); } /** * Print a short to standard output and then terminate the line. */ public static void println(short x) { out.println(x); } /** * Print a byte to standard output and then terminate the line. */ public static void println(byte x) { out.println(x); } /** * Flush standard output. */ public static void print() { out.flush(); } /** * Print an Object to standard output and flush standard output. */ public static void print(Object x) { out.print(x); out.flush(); } /** * Print a boolean to standard output and flush standard output. */ public static void print(boolean x) { out.print(x); out.flush(); } /** * Print a char to standard output and flush standard output. */ public static void print(char x) { out.print(x); out.flush(); } /** * Print a double to standard output and flush standard output. */ public static void print(double x) { out.print(x); out.flush(); } /** * Print a float to standard output and flush standard output. */ public static void print(float x) { out.print(x); out.flush(); } /** * Print an int to standard output and flush standard output. */ public static void print(int x) { out.print(x); out.flush(); } /** * Print a long to standard output and flush standard output. */ public static void print(long x) { out.print(x); out.flush(); } /** * Print a short to standard output and flush standard output. */ public static void print(short x) { out.print(x); out.flush(); } /** * Print a byte to standard output and flush standard output. */ public static void print(byte x) { out.print(x); out.flush(); } /** * Print a formatted string to standard output using the specified * format string and arguments, and flush standard output. */ public static void printf(String format, Object... args) { out.printf(US_LOCALE, format, args); out.flush(); } /** * Print a formatted string to standard output using the specified * locale, format string, and arguments, and flush standard output. */ public static void printf(Locale locale, String format, Object... args) { out.printf(locale, format, args); out.flush(); } // This method is just here to test the class public static void main(String[] args) { // write to stdout StdOut.println("Test"); StdOut.println(17); StdOut.println(true); StdOut.printf("%.6f\n", 1.0/7.0); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload