mgrex's Profile User Rating: -----

Reputation: 3 Apprentice
Group:
Active Members
Active Posts:
254 (0.11 per day)
Joined:
25-March 10
Profile Views:
2,628
Last Active:
User is offline Yesterday, 09:22 AM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Who Cares
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
0
Icon   mgrex has not set their status

Posts I've Made

  1. In Topic: Possible to pass a String array by value, for Class instance declarati

    Posted 11 Mar 2016

    View Postg00se, on 11 March 2016 - 11:32 AM, said:

    Animal pet1 = selection == 0? new Cat() : new Dog();
    

    is probably what you mean.

    How is the user expected to know what 0 or 1 mean?


    I don't want to use an if-statement, especially if I had 10 species to deal with. Once, I can get through this obstacle, then I can go ahead an refine the program to make it more user friendly.

    In line 13, I initialized selection to 0, for the sake of making this program quick and 'efficient' to test.

    I'd simply like to know if its possible, to pass data stored in any collection, such as an array, by it's exact value, so Java can make an instance of that specific (predefined) Class object. This is primarily for educational purposes, hence the simplicity and the 'silliness' of the programs purpose.
  2. In Topic: Extract data from 2D array with variable column sizes.

    Posted 17 Feb 2016

    View PostNormR, on 17 February 2016 - 06:46 PM, said:

    The lines 1,3,5 look like they should print the length of each row.

    Add another println statement at line 4 that shows the contents of each row:
      System.out.println("row "+ row + " " + java.util.Arrays.toString(edge[row]));
    
    


    I re-made a standalone program, ignoring the any other class for now.

    I added your suggestion to line 35 of the current code:
    public class TwoD_Array_Column_Length {
    
    	public static void main(String[] args) {
    		
    		int numEdges = 0;
    		
    		String[] vertices = { 	"Seattle", "San Francisco", "Los Angeles", "Denver",	// 0 , 1 , 2 , 3
    				  				"Kansas City", "Chicago", "Boston", "New York",			// 4 , 5 , 6 , 7
    				  				"Atlanta", "Miami", "Dallas", "Houston" };				// 8 , 9 , 10, 11
    	
    		// Edge array has 46 total pairs
    		int[][] edges = { 
    				{ 0, 1 }, { 0, 3 }, { 0, 5 },										// row 0/vertex 0
    				{ 1, 0 }, { 1, 2 }, { 1, 3 }, 
    				{ 2, 1 }, { 2, 3 }, { 2, 4 }, { 2, 10 }, 
    				{ 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 4 }, { 3, 5 }, 
    				{ 4, 2 }, { 4, 3 }, { 4, 5 }, { 4, 7 }, { 4, 8 }, { 4, 10 }, 
    				{ 5, 0 }, { 5, 3 }, { 5, 4 }, { 5, 6 }, { 5, 7 }, 
    				{ 6, 5 }, { 6, 7 }, 
    				{ 7, 4 }, { 7, 5 }, { 7, 6 }, { 7, 8 }, 
    				{ 8, 4 }, { 8, 7 }, { 8, 9 }, { 8, 10 }, { 8, 11 }, 
    				{ 9, 8 }, { 9, 11 }, 
    			    { 10, 2}, { 10, 4 }, { 10, 8 },{ 10, 11 }, 
    			    { 11, 8 }, { 11, 9 },{ 11, 10 } };
    		
    		
    		// row < edges.length, this causes 92 iterations 
    		//		double the value of 46 pairs, whis is wrong
    		for(int row = 0; row < edges.length; row++) {
    			
    			System.out.println("------ \t------ \t------ \t------");
    			System.out.println("------ \t------ \t------ \t------");
    			System.out.println("Row " + row + " has " + (edges[row].length + 1) + " columns.");
    			
    			System.out.println("\nWith Array.toString method: ");
    			System.out.println("Row "+ row + " has value" + java.util.Arrays.toString(edges[row]) + " pairs.\n");
    			
    			// This inner loop will iterate based on number of column each row has
    			for(int col = 0; col < edges[row].length; col++) {	// 'column'			
    				
    				System.out.println("******ENTER Inner loop******");
    				System.out.println("Current Row holds the value: " + row 
    						+  "\n   While its column holds the value: " + edges[row][col]);
    				
    				System.out.println("******LEAVE Inner loop******");
    				numEdges++;
    			}
    		}		
    	}
    }
    
    


    Output:
    Spoiler


    On line 29, changing, for(int row = 0; row < edges.length; row++), to for(int row = 0; row < 12; row++), since there are 12 row supposedly, output was:


    Spoiler


    It seems I have to take a step back now, and find how to retrieve, a row size of 12, instead of resorting to, using a constant.

    edges.length, thinks there are 46, when this is the amount of pairs in the two dimensional array.
  3. In Topic: Extract data from 2D array with variable column sizes.

    Posted 17 Feb 2016

    View PostNormR, on 17 February 2016 - 06:20 PM, said:

    Ok, that print out shows that the lengths of the rows are different. Are the values shown correct?
    What does that have to do with solving the problem?


    No, it displays 3, as the columns size for every row.

    The column size, would hopefully determine how many times the inner/second for loop should iterate, as the outer loop goes through the 12 rows (0 to 11).
  4. In Topic: Extract data from 2D array with variable column sizes.

    Posted 17 Feb 2016

    View PostNormR, on 17 February 2016 - 06:01 PM, said:

    Explain what is wrong and show what the output should be.
    There isn't enough code here to compile, execute and test to see the problem.

    Have you fixed this error?

    Quote

    java.lang.ArrayIndexOutOfBoundsException: 2


    Why doesn't the code use the rowLength value to control the loop as it looks at the column's contents?


    My bad, sorry.
    edges[c].length, and, rowLength (which equals, edges[row].length;), output the same result.

    All column sizes are displayed as 2 (consisting of index 0,1,2, 3 columns total)

    		for(int row = 0; row < vertices.length; row++) {
    			
    			int rowLength = edges[row].length;	
    			
    			System.out.println("Row " + row + " has " + (rowLength+1) + " columns.");
    			
    			for(int c = 0; c < rowLength; c++) {	// 'column'			
    				graph1.addEdge(row, edges[row][c]);
    				System.out.println(row + " " + edges[row][c]);
    				numEdges++;
    			}
    		}
    
    


    Output should be (i+1, i.e.: start from 1 not 0):

    Row 0 has 3 columns.
    Row 1 has 3 columns.
    Row 2 has 4 columns.
    Row 3 has 5 columns.
    Row 4 has 6 columns.
    Row 5 has 5 columns.
    Row 6 has 2 columns.
    Row 7 has 4 columns.
    Row 8 has 5 columns.
    Row 9 has 2 columns.
    Row 10 has 4 columns.
    Row 11 has 3 columns.

    Right now my main goal is to output the correct column size for each row.
  5. In Topic: Extract data from 2D array with variable column sizes.

    Posted 17 Feb 2016

    View PostNormR, on 17 February 2016 - 05:56 PM, said:

    Have you solved your problem now?


    No.

    Not all the column sizes are 2 or (2+1 = 3).

My Information

Member Title:
D.I.C Regular
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

mgrex hasn't added any friends yet.

Comments

mgrex has no profile comments yet. Why not say hello?