How to implement indexOf method in single linked list? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Below is my code in Java
public int indexOf(E item){
Node<E> node =head;
for(int i=0; i<size;i++) {
node=node.next;
if(node.data==item){
return i+1;
}
}
return -1;
}
Is my code right or do I need to improve it?

Is my code right ...
That depends on your requirements. If you are required to implement the java.util.List API, then the javadoc says this:
"... More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index."
You are using == instead of equals ... which is incorrect.
If you are not required to implement List then we cannot comment on the correctness of == ... or for that matter on any aspect of the code. (For instance the requirements could say that list element positions start at 1!!)
... or do I need to improve it?
If it doesn't meet the requirements, you need to fix that. (But we don't normally describe bug fixes as "improvements".)
If you are asking for a code review, such questions are better asked on the code-review site.
But stylistically, I wouldn't use a for loop. I'd use a while loop, and use node == null as the loop termination condition. (Your code has an implicit dependency on size being correct. If it isn't you may get NPEs.)

If data is not a primitive type, to ensure a thorough comparison you should use node.data.equals(item).

Related

Need Advice: Java Anti If Statement Anti Loops [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is only my third program in java. I'm stuck on this part of the problem.
"The formula is not valid if T > 50 in absolute value or if v > 120 or < 3"
I'm not sure how to translate this into code while restricted to use the following:
no, if statements
no, loops
no, importing
no, new classes to be added
Thank you!
public boolean validateFormula(int T, int v) {
return !(abs(T)>50 || v > 120 || v < 3);
}
In giving you a direct answer to this problem, you might not be able to replicate it next time, or to be able to think for yourself.
In import java.util.* , Math.abs(int n) will return the absolute value of n. The || operator means 'or'. I will not assume that you know the basic structure of an if loop so here:
if(condition || condition || condition){
//execute code here
}
You have everything that you need to solve this, good luck. Furthermore, if you do have any more simple questions like this, I would suggest doing a google search instead.

get subset of elements from set in circular way [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Consider a set with like 100 elements.
Set<String> originalSet; //[1....100] size is 100
From originalSet, (m) subset of elements of some size(n) with some starting index(i) have to retrieved.
Example:
m = 4, n = 45, i = 1
Following have to be retrieved
subset1[1-45], subset2[46-90], subset3[91-35], subset4[36-80]
Whats the best way of doing this.
First of all, Set is unordered so it does not make sense to talk about indices etc. List would make more sense here.
Next, you have to be explicit about what you mean by "best". Performance on insertion? Random access? Creation of your n-from-i subset? These are important question to choose the implementation.
I think two primary options would be linked list with special handling of the last element in subList operation or an array-based list.
Assuming your set has some notion of order, you could write this with
Iterable<Iterable<String>> slices =
Iterables.limit(
Iterables.partition(
Iterables.skip(
Iterables‌​.cycle(originalSet),
i),
n),
m);
If you wanted a set out of this, you'd have to do a transform or something; if you have Java 8 that'd just be something like Iterables.transform(..., ImmutableSet::copyOf).

Getting a collection of successors in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In this method, I'm trying to get the collection of successors from the current one. This is for the Tents and Trees game in Java where I extract the pattern from a file to build the board. '%' represents a tree, '.' represents an empty spot, '^' represents a tent, '#' represents the grass.
public Collection<Configuration> getSuccessors() {
Collection<Configuration> successors = new LinkedList<Configuration>();
return successors;
//return new ArrayList<>(); // replace
}
I don't know if this is the right way, but it doesn't return anything when I run the program. What am I doing wrong?
You are creating an empty LinkedList and returning it. This LinkedList has 0 elements, so you return anything.
You need to insert something in the LinkedList.
I hope my answer will be helpful.

Groovy remove value from collection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In groovy is there a specific way to remove a value from a collection. For example I have a list of form fields but two of them are hidden fields and I'm trying to figure out how to remove them from the collection. The two parameters I'm trying to remove are salesKey and topicSelection. Groovy newbie so code samples are most helpful
request.requestParameterMap.collect { key, value -> "$key: ${value[0].string}" }.join("\n")
key.remove("salesKey")
key.remove("topicSelection")
I think you could use findAll:
request.requestParameterMap.findAll { key, value ->
!( key in ["salesKey", "topicSelection"] )
}
Check out this answer.
Also, depending on your specific aims, there are a couple of other ways to remove a pair, including dropWhile (which is more or less iterating over your data struct) and minus (which isn't so much removing a pair as creating a new structure without the specified pair). Official doc here.

I need a three-valued logic in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried to return null as third value in my boolean function, but it won't compile. I need to return three values from my class method - true, false and null (for example). Is there any standard way how can I do it?
Please use an enumeration with three values defined. Hacking things together is no solution.
Similar question has been asked, it should help.
You can make your own POJO object with this logic in getXX() method. From your method return this POJO with value and test it in code.
Generaly, don't use null values as state indicators.

Categories

Resources