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 5 years ago.
Improve this question
I am trying to create a depth first search algorithm here, kind of. Instead of traversing and finding out how many values it had to go through, it should be finding the highest value in each row.
I know how to actually implement the search and everything but the one part I can't figure out, especially since googling it doesn't work very well is how to create an array of Sub Classes/childs.
I don't really need to know how to initialize a general array, but rather how to initialize an array when I have a class of Node with 2 int values, and then I want an array of said Nodes with the 2 values in them
e.g. right now I have
Node Node1 = new Node(1,1);
Node Node2 = new Node(2,3);
Node Node3 = new Node(2,2);
Node Node4 = new Node(3,5);
Node Node5 = new Node(3,3);
Node Node6 = new Node(3,9);
I want to use an array where it would be something like Node[0] = 1,1
or something similar to that - is there any way this could be done in Java?
Something like:
Node[] nodes = {new Node(1,1), new Node(2,3)};
Then you could say something like:
nodes[0] = new Node(5,5);
or
nodes[0].setXValue = 1 // Or whatever this method and value would be
etc. But look at the comments also as you could use an ArrayList for more flexibility or use another way of initializing an array as in the link given.
Related
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 1 year ago.
Improve this question
The title may sound confusing, but I think my problem is a lot simpler.
Here's what I have:
// a List containing `Boat` objects
private final List<Boat> placedBoats;
// each Boat contains a List<Coordinate> which represents a position on a grid
public List<Coordinate> getPosition() {
return positions;
}
Is there a way of how I can use the stream API to merge all List<Coordinate> into one big List<Coordinate>?
Yes. You can do it like this.
you stream the boats.
since each boat has a list of coordinates you must flatten those into a common stream
then collect the coordinates into a list.
List<Boat> placedBoats ...;
List<Coordinates> coordinates = placedBoats.stream()
.flatMap(boat->boat.getCoordinates().stream())
.toList(); // or .collect(Collectors.toList()) if pre java-16
Of course, by doing it as above, the source identity of where the coordinates came from (i.e. the individual boats) is lost.
Possible solution:
List<Coordinate> placedBoatCoordinates = new ArrayList<>();
for (Boat boat : placedBoats) {
placedBoatCoordinates.addAll(boat.getPosition());
}
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
I'm new to unit testing . How to write unit test for these type of methods?
private boolean fn(Vertex vertex) {
return vertex.id().toString().split(":").length > 1;
}
Here Vertex is the element of gremlin query .
I have tried to create instance of graph and pass a new Vertex object to the function but not working .
i.e
Vertex vertex = (Vertex) graphTraversalSource.addV("Test").property(id,"Profile:TEST");
Can anyone suggest the ways to test these types of method?
You've asked your question about "unit testing" but your question really seems to be about why:
Vertex vertex = (Vertex) graphTraversalSource.addV("Test").property(id,"Profile:TEST");
doesn't let you create a Vertex that you can test. I'd say the most obvious problem is that you didn't iterate your traversal in any way. In this case you need to call next():
Vertex vertex = (Vertex) graphTraversalSource.addV("Test").property(id,"Profile:TEST").next();
Of course, for your fn(Vertex) function that you want to test, I don't see much point in creating an actual Vertex in a graph database - you could instead just use org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex and do:
Vertex vertex = new DetachedVertex("Profile:TEST", "Test", null);
and then pass that to your function to test.
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 6 years ago.
Improve this question
I am working on an Android app, where I need to get a random object from firebase from a child? How to do it in java?
This is how I would do it, assuming I had a child with n objects:
If I didn't know how the value of n, I would first do this in my listener to get the total number of objects:
long n = dataSnapshot.getChildrenCount();
Then I would generate a random integer, i, between 0 and n. If you don't know how to do this, Google it.
Finally, I would get the ith item from the child:
final ArrayList<MyObject> objects = new ArrayList<>();
for (DataSnapshot child : children) {
MyObject object = child.getValue(MyObject.class);
objects.add(object);
}
MyObject objectToUse = objects.get(i);
"MyObject" should obviously be whatever class you're using.
Am I missing something? Is there a better way to do this? I'm pretty new to Android and very new to Firebase so take what I have to say with a big grain of salt haha.
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).
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
I am working on Huffman coding in Java. I am not getting the point of how and where to store the tree. I know how to make the tree, and then save it as a binary file, but for decoding purpose we will again need that same tree to decode. How can I save that tree in a file to attach it with binary file?
Write the tree as a series of bits: 0 represents a leaf, 1 represents an internal node. The output for a binary tree (Huffman or otherwise) with N leaf nodes and N-1 internal nodes will be a sequence of 2N-1 bits. (You can actually save two bits, since you know that the first and last nodes in the tree will be leaf nodes, but it's probably not worth complicating the algorithm to save two bits.)
Perhaps easiest is to arrange the bits in pre-order:
function write_tree (top_node) {
if is_leaf(top_node) {
write "0"
// optionally, write any date associated with the leaf node
// although in practice it's easier to write the leaf data
// to a separate output stream. That lets this stream contain
// actual bits rather than the characters "0"/"1"
}
else {
write "1"
write_tree (top_node.left)
write_tree (top_node.right) }}
function read_tree (bit_stream) -> returns tree
next_bit = bit_stream.read()
if next_bit = "0" {
root = new leaf
// optionally read data associated with the leaf node
}
else {
root = new internal node
root.left = read_tree (bit_stream)
root.right = read_tree (bit_stream) }
return root }
I didn't notice at first that you mentioned Java, so I wrote the above in pseudo-code, which I'm sure you'll have no trouble re-writing in Java.