Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:14:22+00:00 2026-06-17T10:14:22+00:00

Ok.. so I decided to try linked lists in Java rather than the usual

  • 0

Ok.. so I decided to try linked lists in Java rather than the usual C++ which I’m used to references an pointers in..

Traversal can be Up, Down, Left, Right for a center node. For nodes in the corners, they only move two directions and nodes on the edges can move 3 directions.. All others can move 4.

Problem:

When my program leaves the constructor, my nodes are deleted somehow :S When I use the get/set, it cannot traverse the links because they are null.. Except for the first node.

My node is:

package linkedlist;

public class Node {
    public Node Up, Down, Left, Right;
    public int Value;

    public Node() {
        Value = -1;
        Up = Down = Left = Right = null;
    }
}

I do implementation like:

package linkedlist;

public class Matrix {
    private int Width, Height;
    private Node Reference;

    public Matrix(int Width, int Height) {
        Reference = new Node();
        this.Width = Width; this.Height = Height;
        Node RowIterator = Reference, ColumnIterator = Reference;

        for (int I = 0; I < Height; ++I) {
            for (int J = 0; J < Width; ++J) {
                if (I == 0) {
                    if (J < Width - 1) {
                        RowIterator.Right = new Node();
                        RowIterator.Right.Left = RowIterator;
                        RowIterator = RowIterator.Right;
                    }
                }
                else {
                    if (I < Height - 1) {
                        ColumnIterator.Down = new Node();
                    }

                    RowIterator = ColumnIterator;
                    RowIterator.Right = new Node();
                    RowIterator.Up = ColumnIterator;
                    RowIterator.Up.Down = RowIterator;
                    RowIterator.Right.Left = RowIterator;
                    RowIterator.Right.Up = RowIterator.Up.Right;
                    RowIterator = RowIterator.Right;

                    ColumnIterator = ColumnIterator.Down;
                }
            }
        }
    }

    public void SetValue(int I, int J, int Value) {
          //Same as get except it sets rather than returns..
    }

    public int GetValue(int I, int J) {
        RowIterator = ColumnIterator = Reference;
        for (int K = 0; K < J; ++K) {
            for (int L = 0; L < I; ++L) {
                RowIterator = RowIterator.Right;
            }

            ColumnIterator = ColumnIterator.Down;
            RowIterator = ColumnIterator;
        }

        return RowIterator.Value;
    }
}

And the main like:

package linkedlist;

public class LinkedList {
    public static void main(String[] args) {
        Matrix M = new Matrix(6, 6);

        M.SetValue(3, 3, 10);
    }
}

So when I attempt to set the value at near middle of the matrix, it throws a null pointer error.. If I attempt to set it in the constructor, it works just fine.. Thus my nodes must be somehow getting garbage cleaned..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:14:23+00:00Added an answer on June 17, 2026 at 10:14 am

    You iterate across the row as many times as you iterate down the column. I.e., in your example, the actual node that is accessed is (9, 3), out of the bounds of your Matrix.

    Instead, you should iterate once across the row, and then once down the column.

    for (int K = 0; K < J; ++K) {
        Iterator = Iterator.Down;
    }
    
    for (int L = 0; L < I; ++L) {
        Iterator = Iterator.Right;
    }
    

    Any particular reason you aren’t just using a 2-dimensional array?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Finally decided to try Flash Builder rather than FlashDevelop and got caught up on
[SOLVED] So I decided to try and create a sorted doubly linked skip list...
I decided to try to my hand at this, and have had a somewhat
Having decided to try AForge for video and imaging stuff, I tried to implement
i am new to scrapy and decided to try it out because of good
A few days ago I installed tomcat 7 and decided to try building a
I'm pretty new to PHP, but I decided to try and make a simple
In my self-imposed quest to learn Netbeans, I decided to try out an already
I have a working WAMP environment (Apache Friends). I decided to try Subversion and
After failing to achieve this with link_to remote, I decided to try jQuery way.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.