I have a node class defined as below, but I keep on getting the error in Eclipse
void is an invalid type for the variable connectNode
Please explain why?
class Node{
char label;
boolean visited = false;
public Node (char l){
this.label=l;
}
public String toString() {
return Character.toString(label);
}
}
I have defined ArrayList as below:
ArrayList<Node> nodes = new ArrayList<Node>();
I'm trying to use the following method to print the values of Start and End index
public void connectNode(Node start,Node end){
int startIndex=nodes.indexOf(start);
int endIndex=nodes.indexOf(end);
System.out.println(startIndex);
System.out.println(endIndex);
}
Your error usually arises when you try to declare a method inside another method.
Recheck your code and file structure.
Is it possible you're doing something like:
nodes.add(connectNode(start, end));
If that's the case then it means you're trying to insert something that is not suitable for this array list. More specifically you try to put void in a list of type Node.
Related
I have a linked list of n boolean elements and i should write a method that returns the logical AND of those elements.
public class Element{
boolean bool;
Element next;
public Element(boolean bool, Element next){
this.bool=bool;
this.next=next;
}
}
public class List{
Random rand= new Random();
Element e=null;
public List(int n){
for(int i=0; i<n; i++){
e= new Element(rand.nextBoolean(),e);
}
}
I thought about creating an array, loop through the list, fill the array with the logical AND values, then return the array.
Honestly i don't think it would be a good solution for various reasons.
Can i do that without arrays?
Just use Java's LinkedList and contains :
LinkedList<Boolean> myList = new LinkedList<>();
// add a bunch of true's
myList.add(true);
myList.add(true);
myList.add(true);
myList.add(true);
System.out.println("logicalAND="+!myList.contains(false));
// add a false
myList.add(false);
System.out.println("logicalAND="+!myList.contains(false));
prints
logicalAND=true
logicalAND=false
If your list element is more complex but still needs to resolve equality on the Boolean value then be sure to update its equals method.
Forgive me if I'm misunderstanding the question here, but if you're trying to get the boolean and of a whole chunk, isn't that just true if everything is true, false otherwise?
Here's some vague code of my thinking
boolean value=true;
for(int i=0;i<n;i++){ //traverse the nodes
if( /*Value at the node you're at*/==false )
value=false;
}
return value;
You could use recursion and write a Method in your Element class like this:
boolean andAll() {
return (next!=null) ? bool && next.andAll() : bool;
}
If bool is false, Java won't evaluate the part on the left side of that expression.
You can write an interface method in your list class to call that method on your first list Element.
If your target isn't creating a new implementation of linked list, you should use the LinkedList delivered by Java and it's contains method.
Greetings
I need to add an "user" into a linked list, and the linked list is a part of a linked list.
public void shibutz(user a){
this.serverList(this.placeInServerList(a.code)).add(a);
server list is the linked list of linked lists.
these are the classes this line uses:
public int placeInServerList(String code){
if (this.serverList.contains(code)){
return indentifyCode(code);
}
return -42;
}
public int indentifyCode (String code){
int counter=0;
Group i= this.serverList.getFirst();
while (this.serverList!=null){
if (i.getCode()==code){
return counter; }
counter++;
i=this.serverList.get(counter);
return indentifyCode (code);
}
return -42;}
group is a linked list.
now, I need help with the first code part. I don't understand what Eclipse wants from me. none of the solutions it's giving me are relative. thanks!!!
Reading this line :
Group i = this.serverList.getFirst()
I assume that serverList is a LinkedList<Group>.
Hence, this is invalid :
public int placeInServerList(String code){
//code is a String, not a Group
if (this.serverList.contains(code))
As serverList is a LinkedList<Group>, the method that you can call is serverList.contains(Group). The methodserverList.contains(String) does not exist.
I have a problem that is pretty basic but I am struggling to get right. Basically, I have a constructor that has some methods defined on it using this.. I want to pass one of those methods a paramter, but I am struggling to declare it in a way that doesn't cause an error. This is my code:
public class Graph {
public Graph(int[][] gA) {
boolean[] visited = new boolean[gA.length];
Arrays.fill(visited, 0, gA.length, false);
//this is the bit I'm struggling with:
this.adj(int v) = gA[v];
this.array = gA;
this.visited = visited;
}
}
How do I get this.adj to accept a parameter? I also tried creating a method declaration but couldn't get this work either. Is there some kind of design pattern I should use?
Thanks
EDIT: Apologies - made a mistake in the code excerpt. this.adj[v] should be returning a row of the gA array, which it only has access to within the constructor, so I cannot move the function outside.
This :
this.adj(int v) = adj(v);
Is the wrong way. Just use:
adj(v); // Call the method adj with the parameter v
Since you are calling it in the constructor, hence it does not matter if the method is static or not. A constructor can call both of them.
Edit:
I want adj[v] to return the row of gA at v. I've edited the code above
You can do:
gA[v] = adj(v);
why you dont just call the method you defined??
public class Graph {
private YOUR_TYPE adj;
public Graph(int[][] gA) {
boolean[] visited = new boolean[gA.length];
Arrays.fill(visited, 0, gA.length, false);
//this is the bit I'm struggling with:
this.adj = adj(v);
this.array = gA;
this.visited = visited;
}
YOUR_TYPE adj(int v){
return .... something from YOUR_TYPE
}
}
Simple Linked List
public class List_manager {
Entry first;
Entry last;
public void add(String el) {
if (isEmpty()) { first=new Entry(el); last=first; return; }
new Entry(el,last);
}
public String get() {
Entry temp=first;
first=first.next;
return temp.data;
}
public boolean isEmpty() {
return first==null;
}
private class Entry {
String data;
Entry next;
public Entry(String data,Entry to) {
this.data=data;
to.next=this;
to=this;
}
public Entry(String data) {
this.data=data;
}
}
}
#The main class#
I added 3 element and list contains only 2... why?
public class Main {
public static void main(String[] args) {
List_manager l=new List_manager();
l.add("1");
l.add("2");
l.add("3");
System.out.println(l.get());
System.out.println(l.get()); // Why here output: "3"??
System.out.println(l.get()); // here is an error occurs
}
}
I really don`t get why list contains 2 elements?
Why it ignores 2nd added element?
to=this; This sentence have no influence on variable 'last', because veriable 'to' is formal parameter, while variable 'last' is actual parameter. So, when you executed this sentence "to = this;" the value of
variable 'last' was not changed to next.That's mean variable 'last' always pointed to the first element.
my change is : new Entry(el,last); --> last = new Entry(el,last);
Things look better.
Think about what your get method is doing. You already noticed some aberrant behavior with it.
public String get() {
Entry temp=first;
first=first.next;
return temp.data;
}
What happens the first time I call this?
temp gets whatever first is pointing to
first is moved to its next element (RED FLAG)
temp's data is returned...
One problem is that you're moving your head reference around - this is a bad idea, since it means that you can never access the true first element in your list ever again.
Now on its own, even with this implementation, you should still be able to get the first element.
The above was just a red herring - although you should not be moving your head pointer around. This is the real problem. What happens on subsequent add calls to your list?
public void add(String el) {
if (isEmpty()) {
first = new Entry(el);
last = first;
return;
}
new Entry(el,last);
}
Only the first element inserted and the last element inserted are respected. All other entries after next are overwritten.
I suggest that you use a debugger to figure this one out, as it stems from a misunderstanding of a good approach to do this. You only want to insert things through your tail pointer once you have one element. Doing this through object creation only causes heartache and confusion.
For posterity, I'll leave you with a sample, verbatim implementation I wrote for a singly linked list implementation I did a while back. It describes a more viable approach to inserting into a list.
public void insert(E data) {
Node<E> candidate = new Node<>(data);
if(head == null) {
head = candidate;
tail = head;
} else {
tail.setNext(candidate);
tail = tail.getNext();
}
size = size + 1;
}
I am writting a program that performs an a star search throughout a map. I have created a class that hold all the nodes of the map.
public Node {
Node up_node, right_node, down_node, left_node;
}
public class Star {
public static void main(String args[]) {
Node a=new Node();
Node b=new Node();
Node h=new Node();
Node here=new Node();
Node[] NextNode;
NextNode = new Node[10];
for(int i=0;i<10;i++) {
NextNode[i]=new Node();
}
int j=0;
a.up_node=h;
a.right_node=b;
b.left_node=a;
h.down_node=a;
//if certain conditions are met
NextNode[j].here_node=a.up_node;
//what i was hoping to do is copy the node a.up which is h
}
}
into NextNode[0] in this case. However it keeps returning a memory address of some sort : test.Node#10b28f30: test being the name of the package, please help!
#override the toString() method to display the internal property of your class.
By default, java display the full class name#hashCode value.
Variables in Java are object references not actual objects. NextNode[j].here_node = a.up_node; will make NextNode[j].here_node and a.up_node point to the same object. Is this not what you want?
If you wanted to make a completely new copy of the object, then you can implement that in the Node class:
public class Node {
Node up_node, right_node, down_node, left_node;
public Node clone() {
Node ret = new Node();
// copy the properties
ret.up_node = this.up_node;
...
return ret;
}
}
Now
NextNode[j].here_node = a.up_node.clone();
will make a copy (although it's only a shallow one -- the copy will point to the same objects via its fields as opposed to copies of them).
I assume your confusion about the code returning "an address" comes because you tried to print a node, e.g.
System.out.println(a.up_node);
You'll get something like test.Node#10b28f30, but try
System.out.println(NextNode[j].here_node);
and you should get exactly the same string, indicating that they're pointing to the same object.
To get something nicer, you must override Node's implementation of toString(). Here's an example that will give each Node a unique number:
public class Node {
Node up_node, right_node, down_node, left_node;
// how many nodes were created
private static int count = 0;
// the number of this node
private int number;
public Node() {
// increment the number of nodes created
++Node.count;
// assign that number to this node
this.number = Node.count;
}
public String toString() {
return "Node #" + this.number;
}
}
We know that every class that we write are child of Object class. When we print a child of an Object it prints its toString() method. By default it is a hashed value of memory location. So it prints sort weird things. if we #overriding toString method to return something more meaningful to us then we can solve this problem. If we can name our node class someway I think we can keep track of them easily
class Node(){
String nameOfNode;
//contractor to keep track of where it goes.
public Node(String a){
nameOfNode=a;
}
//when we will print a Node it is going to print its name
public String toString(){
return nameOfNode;
}
}
Then it will print the name of the node. And it will stop showing that weird memory address.
and replace your new Node() with distinct name new Node("a name")