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

Reputation: 3 Apprentice
Group:
Active Members
Active Posts:
238 (0.11 per day)
Joined:
25-March 10
Profile Views:
2,489
Last Active:
User is offline Yesterday, 10:33 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: Attempted to create a STATIC generic type ArrayList, by couldn't

    Posted 1 Feb 2016

    View Postandrewsw, on 01 February 2016 - 05:39 AM, said:

    It looks to me like you've overridden size() with root.count() which I'd guess is returning the 1.

    This is where you need to spend some time setting breakpoints and stepping through your code.



    In the debugger, count, correctly increments, and eventually reaches 13.

    ArrayList *.size stays at, "<errors during evaluation>". I use the 'Step Into' button in Eclipse debugger to run the program one statement at a time.

    None of the the System.out.prinln displays in the output from the following fuction.

    	public int count() {
    				
    		int result = 1;
    		
    		System.out.println("BTNode.count: " + result);
    		
    		if (left != null){
    			
    			System.out.println("BTNode.count: " + result);
    			result += left.count();
    		}
    
    		if (right != null){
    			
    			System.out.println("BTNode.count: " + result);
    			result += right.count();
    		}
    
    		System.out.println("BTNode.count: " + result);
    		
    		return result;
    	}
    
    
  2. In Topic: Attempted to create a STATIC generic type ArrayList, by couldn't

    Posted 31 Jan 2016

    View Postandrewsw, on 31 January 2016 - 07:46 PM, said:

    Personally, I would go back to the generic, non-static, code, and discover why size() returns 0. Post the code that caused this behaviour.



    With the following, I tried, to call the index 0 of the ArrayList:
    	public void display_traversedList() {
    		System.out.println("\n[display_traversedList called]");				// for debugging
    		
    		System.out.println("[ArrayList object size: " + traversed_treeNodes.size() + "]");		
    		//for(int i = 0; i < traversed_treeNodes.size(); i++)
    			System.out.print(traversed_treeNodes.get(0) + " ");				// must be static to remember added values
    		
    		System.out.println("\n[display_traversedList for loop called]");	// for debugging
    	}
    
    


    I got the following message, "Array out of bounds exception"

    Pre-order   ::  Element added: 96 , ArrayList size: 1 Element added: 48 , ArrayList size: 1 Element added: 45 , ArrayList size: 1 Element added: 23 , ArrayList size: 1 Element added: 36 , ArrayList size: 1 Element added: 64 , ArrayList size: 1 Element added: 55 , ArrayList size: 1 Element added: 256 , ArrayList size: 1 Element added: 180 , ArrayList size: 1 Element added: 125 , ArrayList size: 1 Element added: 120 , ArrayList size: 1 Element added: 768 , ArrayList size: 1 Element added: 720 , ArrayList size: 1 Element added: 956 , ArrayList size: 1
    [display_traversedList called]
    [ArrayList object size: 0]
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    	at java.util.ArrayList.rangeCheck(Unknown Source)
    	at java.util.ArrayList.get(Unknown Source)
    	at javafoundations.LinkedBinaryTree.display_traversedList(LinkedBinaryTree.java:237)
    	at Ch_17_01_BSTree_Add_Sort_Numbers.main(Ch_17_01_BSTree_Add_Sort_Numbers.java:28)
    
    
    


    The following function seems to correctly perform [element added] operation to the array list object, but the size is always displayed as 1, instead of 14, when the last element/number is added.

    	public void traversedList(T element) {			
    		
    		traversed_treeNodes.add(element);
    		System.out.print(" Element added: ");							// for debugging
    		System.out.print(traversed_treeNodes.get(0) + " ");				//
    		System.out.print(", ArrayList size: " + traversed_treeNodes.size()); // + "]");	
    	}
    
    


    This is the class, that contains the main methods in question, along with the ArrayList object:

    Spoiler
  3. In Topic: Initialize a class instance using pre-existing array subscripts.

    Posted 29 Jan 2016

    View PostNormR, on 29 January 2016 - 04:31 PM, said:

    Quote

    Both result in nullpointer exception.

    If you need help with that, copy the full text of the error message and paste it here.
    The message gives the line number where the exception happened. Look at that line and find the variable with the null value. You may need to use the println statement to display all variables values. Then backtrack to find out why the variable does not have a valid value.

    Can you explain what you want to create more than one instance of the BST? Why isn't one enough?


    It seems, I approached this project from the wrong angle.

    The previous exercise that the authors performed was based on decision tree, that did not have automatic sorting. The decision tree was based on a health diagnosis:

    Posted Image

    In this case, it is crucial that the programmer specifies precisely where each individual nodes should go. Furthermore, the authors were creating multiple instances of the binary tree, it seems.

    In the original post, the numbers are added to the left or right of the root and sub-tree based, on whether the number is larger or smaller. Therefore, it's not crucial or wise for the programmer to manually specify where each node should go.

    I was able to solve the problem. Thank you for the assistance.

    The following statements sufficed:

    		for (int i = 1; i < numbers.length; i++) {	
    			tree.add(numbers[i]);
    		}		
    
    
  4. In Topic: Initialize a class instance using pre-existing array subscripts.

    Posted 29 Jan 2016

    View PostNormR, on 29 January 2016 - 02:53 PM, said:

    Quote

    The program functioned as desired...

    I don't see any questions. Is the program working now?


    No.

    I took your advice from the 4th post:

    theMap.put(node_array[i], new LinkedBinarySearchTree<Integer>());
    


    And edited it to the following, by assigning a value to the key, which I assume the argument before the comma.

    listOf_nodes.put(node_array[i], new LinkedBinarySearchTree<Integer>(numbers[i]));
    


    Both result in nullpointer exception.

    I'd like to use the for loop, to create, name, and assign node values, for a binary search tree.

    Spoiler
  5. In Topic: Initialize a class instance using pre-existing array subscripts.

    Posted 29 Jan 2016

    View Posthorace, on 28 January 2016 - 03:17 PM, said:

    try putting a debugging statement on line 25, e.g.
    System.out.println(listOf_nodes + " " + i + " " + node_array[i] + " " +numbers[i]);
    

    it may help identify which object is null


    I added the System.out statement before and after the '.put' method declaration.

    Spoiler


    The output was:
    null 1 n1 256
    Exception in thread "main" java.lang.NullPointerException
    	at Ch_17_01_BSTree_Add_Sort_Numbers.main(Ch_17_01_BSTree_Add_Sort_Numbers.java:27)
    
    
    


    The author assigned value to the tree nodes of a previous exercise, dealing with 'decision trees', through the following means:

    Spoiler


    Again, what I'm trying to similarly do, is condense lines 47 through 57, in the preceding block of code, by using a for loop to add data, instead of writing multiple repetitive lines.

    As we see with 'nX = new LinkedBinaryTree<String>(eX'), there are 11 instances of the LinkedBinaryTree<String>, node class, declared.

    The program functioned as desired...

My Information

Member Title:
D.I.C Head
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?