Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Modify class fields

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Modify class fields

    Hello,
    I am trying to remove values from lists.
        private List<Node> nodes;
        private List<Edge> edges;
     
     
        public List<Edge> getEdgeTo(Node x) {
            List<Edge> edges1 = new ArrayList<Edge>();
            for (int i = 0; i < edges.size(); i++) {
                if (edges.get(i).getFirstNode().getId() == x.getId() || edges.get(i).getSecondNode().getId() == x.getId()) {
                    edges1.add(edges.get(i));
                }
            }
            return edges1;
        }
     
        private void removeEdge(Node x) {
            List<Edge> edges2 = getEdgeTo(x);
            for (int i = 0; i < edges2.size(); i++) {
                edges.remove(i);
            }
     
        }
     
        private void removeNode(Node x) {
            for (int i = 0; i < nodes.size(); i++) {
                if(nodes.get(i).getId() == x.getId()){
                    nodes.remove(x);
                }
            }
     
        }

    But when i try to run method that uses this functions it works
    incomprehensible to me.
    public Map<Node, Node> findNextHospital(Node start) {
            Node current = start;
            Map<Node, Node> minDistances = new HashMap<Node, Node>();
            calculateShortestPathFromSource(current);
            Node nextHospital = getMinimumDistanceOfAll(current);
            System.out.println("Next hospital " +nextHospital.getId());
            minDistances.put(current, nextHospital);
            removeNode(current);
            removeEdge(current);
            System.out.println("Before");
            System.out.println("Nodes size "+ nodes.size() + "Edges size"+edges.size());
            for(int i=0;i<nodes.size();i++){
                System.out.println(nodes.get(i).getId());
            }
            for(int i=0;i<edges.size();i++){
                System.out.println(edges.get(i).getSecondNode().getId() + " " + edges.get(i).getFirstNode().getId());
            }
     
            current = nextHospital;
     
            System.out.println("current id "+ current.getId());
            calculateShortestPathFromSource(current);
            Node nextHospital1 = getMinimumDistanceOfAll(current);
            System.out.println("Next hospital "+nextHospital1.getId());
            minDistances.put(current, nextHospital1);
            removeNode(current);
            removeEdge(current);
            for(int i=0;i<nodes.size();i++){
                System.out.println(nodes.get(i).getId());
            }
            for(int i=0;i<edges.size();i++){
                System.out.println(edges.get(i).getSecondNode().getId() + " " + edges.get(i).getFirstNode().getId());
            }
            System.out.println("After");
            System.out.println(nodes.size());
            System.out.println(edges.size());
            return minDistances;
        }
    Result of running this function looks like this:
    Next hospital 3
    Before
    Nodes size 4Edges size10
    1
    2
    3
    4
    3 0
    2 1
    4 3
    0 1
    0 3
    3 1
    1 2
    1 4
    3 4
    4 2
    current id 3
    Next hospital 0
    1
    2
    4
    2 1
    0 1
    3 1
    1 4
    4 2
    After
    3
    5
    2

    Numbers of nodes and edges have decreased but list still containts some edges to Node with id 0 despite of fact that all should be removed. Also node with id 0 when I check if nodes list containts it returns boolean false value. But then "Next hospital 0" has id 0 but node with this id shouldn't exist.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Modify class fields

    How can the code be compiled and executed for testing? It is missing some parts.

    What is the desired output for the program?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Modify class fields

    i tried using edges.removeAll(edges2) instead of previous method and it works fine to my surprise now

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Modify class fields

    I'm glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple Question (About Resetting a Class' Fields)
    By dpapi95 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 1st, 2014, 04:28 AM
  2. Modify a class and test
    By SOLAS in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 8th, 2013, 07:55 AM
  3. Replies: 1
    Last Post: February 12th, 2012, 01:01 AM
  4. can't compare class fields
    By r0x in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 27th, 2011, 08:06 AM
  5. Cannot set class fields using textInput
    By javascrub in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 04:04 AM