2

Why is this wrong? I can't use add, I am not sure how to. Some java documentation says I need to have add(index,data) but others are just add(data) and the compiler is support that as well. It is having an error with my data type.

import java.util.*;
public class graph1 {

    public static void main (String[] args){
        ArrayList<Node> web = new ArrayList<Node>();    
        web.add(0, "google", new int[]{1,2});

    }
}

Node.java:

 public class Node {
        int i;
        String title;
        int[] links;    

        Node(int i, String title, int[] links){
            this.i = i;
            this.title = title;
            this.links = links;
        }
    }
5
  • Just to help you out, work through the Language Basics here: docs.oracle.com/javase/tutorial/reallybigindex.html. You mentioned on your other question not being too advanced, but you're working through things like creating and using objects before understanding basics like arrays. Focus on conditionals, loops, arrays and then move on to objects/classes. Commented Aug 21, 2013 at 20:02
  • 1
    I actually have about a year of experience with Java I am just very bad at programming. Commented Aug 21, 2013 at 20:13
  • 1
    Go back to basics and learn off primitive types, conditionals, loops and arrays. Without them, you'll just get more confused with objects, classes and the like Commented Aug 21, 2013 at 20:15
  • 1
    I am very familiar with all of that. Commented Aug 21, 2013 at 20:18
  • 1
    In your other question you didn't know how to initialise an array. I'm just saying, in my opinion, it will help a lot. Anyways, good luck! Commented Aug 21, 2013 at 20:26

5 Answers 5

4

You're forgetting to include new Node(...) inside of the ArrayList's add(...) method since you're not adding the combination of an int, a String and an array of int to the ArrayList, but rather you're adding a Node object. To do this, the Node object must be explicitly created and then added:

web.add(new Node(0, "google", new int[]{1,2}));
Sign up to request clarification or add additional context in comments.

2 Comments

Oh ok I wasn't sure how java handled data types like this. I didn't know if it was implied that what I was putting in was a Node or not.
@YKQ56: it's not. The compiler has no idea what type of object you're trying to add until you explicitly tell it. The compiler must then check to make sure that it's the correct type of object.
2

Use this:

web.add(new Node(0, "google", new int[] {1, 2}));

Comments

2

You need to make the node like this

Node node = new Node(i, title, links);
web.add(node);

1 Comment

To help explain this code. What you have is an array list of Nodes. A node is an object, which has an int i, a String title and an array of ints. So, when you add to your list you actually want to add a node, rather than any other data. You can do this using exactly what Andrew says. It creates a new node named "node", then adds it to the list. To make this work with the values you provided, you need: Node node = new node (0, "google", new int[]{1,2}); web.add(node);
1

You have an arraylist of Nodes, but are trying to add a bunch of random variables. You need to use those variables to make a Node and then add that.

web.add(new Node(0, "google", new int[]{1,2}));

Comments

-1

Your custom class has to be instantiated in order to add it to the ArrayList. To do this, use web.add(new Node(0, "google", new int[]{1,2}));.

In your case, you used web.add(0, "google", new int[]{1,2});, which java compiler understood as you were trying to add 3 object at once, thus compiler complained that something is wrong with your code.

Also, you should consider implementing (overriding) custom compare(o1, o2) if you'll need to sort the array, because the default Collections.sort(list) doesn't know how to correctly order your objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.