in my class is implement a heap and use some of the methods we were provided. The methods I was provided to code and use are "siftDown", "isEmpty" and "heapify". I'm pretty sure the code I have written for "heapify" and "isEmpty" is correct, where I think I am finding fault is the code for my "siftDown". Would you mind taking a look at my code and see why when adding integers to the heap object that I have created in the main code, that they are not being output correctly?
public class Tester {
public static void main(String[] args) {
Heap myHeap = new Heap();
myHeap.insert(9);
myHeap.insert(15);
myHeap.insert(6);
myHeap.insert(4);
myHeap.insert(10);
myHeap.insert(9);
myHeap.insert(3);
When I add an element to my array, I have to make sure that it stays a heap, ie every child is smaller than its parent. However the method that I am using for this, trickling up, is not updating the elements properly, it pretty much just leaves is as is.
Here is the relevant code:
public class MaxIntHeap { int[] array; int actualSize = 0; public MaxIntHeap(){ array = new int[20];
I am in the process of implementing Priority queue, as I understand that there are many data structures you could use to implement. I implemented it with the an array, which it works absolutely fine. However I have limitations on what collections I can use from the collections classes. I fact I cant use any of the collections classes. Meaning I cant use array.
I’m trying to implement Priority Queue using heap. And implementing heap using binary trees. But however I have a few questions which I need to clarify and I cant think of any other way of resolving it. Ofcourse I can implement my own simple array class using linked list.
Inserting into heap would be quite simple, as I just need to find the right last position from left to right leaf to insert the node into the tree. However after inserting, you may want to make sure that leaf node values are > than root node. Therefore, the root node will always be with the highest priority.
I call these steps where you compare from top down as bubbledown and bubbleup. To do this I really need a for each node within the treee node to have attribute pointing to its root node. So in case of bubbleup I always have a pointer for a given node to its root, without it would mean I would to traverse through the entire tree to identify its root. Which I believe is very inefficient.
Or have I taken this completely wrong? Or is it the case that heap are only best with arrays and therefore use array (by implement it using linked list?)
maximum heap size is set at 1.5GB and consumption of memory at peak load is about 1.1GB. when it reaches 1.1GB, application starts to hang. what could be the problem? shouldn't it be hanging at the point where memory is about equal to the max heap setting? no heap dumps were generated. is this due to server hardware or something? already got the garbage collection data and nothing seemed unusual.
I am learning to use heaps and as an exercise I am trying to write a program using a heap class I have created to sort words. I have read in words from a file and added them to the heap successfully. I am having some trouble figuring out how I can print out a sorted list of the words. From my understanding of how a min-heap works, if I remove from the min/root node they will always be removed in sorted order. So far I have tried out to do a simple for loop but, only half of the heap is being removed. Not sure if my logic is incorrect of there is an error somewhere in my removeMin() function specifically in the while loop.
public static void main(String[] args) { Heap heap = new Heap(); heap = read( heap ); for( int i = 0; i < heap.getSize(); i++){ heap.removeMin();
I am using a 64 bit Win 7 Pc with 64-bit JVM and we get the error: Java heap space. So we want to increase the Java heap size but not for one application but for every application or in general.
We tried with the java -xmx command but it didn't work...
We tried setting the system variable JAVA_OPTS but again it didn't work...
I have two unix systems in which on one system I installed sun solaris jdk and on another system I installed IBM jdk.
Java programs which consume more heap memory are getting failed on sun solaris jdk system where as same programs are successfully getting executed on IBM jdk system .
My question is does sun solaris 64 bit jdk needs more heap than IBM 64 bit jdk ??
When I try to write the .xlsx file using apache POI, XSSFWorkbook API and run this program in Eclipse STS, I am getting the java.lang.OutOfMemoryError: Java heap space error. Then I searched the net and add these -Xms512m -Xmx1024m in the eclipse VM arguments. Even though I am getting the same error. Again i increase heap size but i am getting the different error like "occurred during initialization of VM, Could not reserve enough space for object heap". how to increase the heap size or any other API to read, delete and write the .xlsx file. I am having 4GB ram in my machine. Apache POI is very good for .xls but if it is .xlsx performance wise it is very slow.
Given a string, compute a new string where identical chars that are adjacent in the original string are separated from each other by a "*". My implementation :
package com.tcs.dash; public class StringBuild { public String edit(String userIp){ StringBuilder builder = new StringBuilder(userIp); String replaceText = ""; for(int i = 0; i < builder.length() - 1; i++){ if(builder.charAt(i) == builder.charAt(i+1)){ replaceText = builder.charAt(i) + "*" + builder.charAt(i+1); builder = builder.replace(i, i+1, replaceText); } } return builder.toString(); } }
I am getting error at line 13. An exception actually.
I/P given = aaaa
Console:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source) at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source) at java.lang.AbstractStringBuilder.replace(Unknown Source) at java.lang.StringBuilder.replace(Unknown Source) at com.tcs.dash.StringBuild.edit(StringBuild.java:13) at com.tcs.dash.StringBuildExample.main(StringBuildExample.java:14)
I have a simple JavaFX Application that open a Browser and shows google page. After exit the Application and free all objects, I can see that the JavaFX objects like Scene, Stage, WebView and WebEngine are alive in the heap memory after call GC. I can see this objects with JProfiler and other Profiler tools.
This is my Test code: import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javafx.application.Application; import javafx.application.Platform;
To test the application click on Start Button to show google web page, click on Stop Button to stop the application, run a Profiler tool, and call gc, the JavaFX classes are alive. I am using java version "1.7.0_51" and windows 8.1 Is there something wrong in my code? Or this is the normal behavior?
I was wondering where is the memory allocated for an applet; by the browser; by the JVM; some applet specific java option? I get an out of memory error when running my applet (loading pictures).
I am using simple jdbc connection to connect to Sybase as shown above , problem is security team is able to see password as clear text in heap, How to avoid it.
I am working on program and have been struggling to get around step 5 and 6 given below.
I have got on with the first couple of points. Where to begin with steps 5 and 6.
Java Code:
class Hourglass { int height; int bottomHalf; public Hourglass (int h) { height =h; } public Hourglass (){ height=3; }
/*Write a method dropGrain that simulates one grain of sand falling into the bottom half of the Hourglass. If all the sand is already at the bottom before a grain is dropped, this method should cause the hourglass to be flipped, meaning that all the sand will be in the top again. Then, one grain of sand should fall. */
//Hint: this method can be quite short. All you need to do is update one attribute.
public void dropGrain(){ }
/*Write a method getHeapHeight() which returns the height of the heap of sand in the bottom of the hourglass.
Hint: a triangle of height h contains h*h grains (=1+3+5+...+h).
So determining the height when the amount of sand in the bottom half is a square (1,4,9,16,...) is easy. Think about what happens if the amount of sand is not exactly a square.*/
public int getHeapHeight() { } mh_sh_highlight_all('java');
I am not a java developer, but I am using a java code that was available online to convert a large XML file to CSV file. The input file size is big, it is around 3GB. I got an error that it is out of memory, it is expectedly due to the large input file that i am trying to convert. Splitting of this file is not possible,
This is what I ran : xml2csv-conv data.xml data.csv
Error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at com.sun.org.apache.xerces.internal.dom.DeferredDoc umentImpl.createChunk(Unknown Source) at com.sun.org.apache.xerces.internal.dom.DeferredDoc umentImpl.ensureCapacity(Unknown Source) at com.sun.org.apache.xerces.internal.dom.DeferredDoc umentImpl.createNode(Unknown Source) at com.sun.org.apache.xerces.internal.dom.DeferredDoc umentImpl.createDeferredTextNode(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.Abstrac tDOMParser.character
[code]....
Additional information: I am running this from a Windows8 64 bit machine with 8GB physical RAM.
i have to write more than 100000 rows in a excel sheet (file size more than 20 MB) via java.
when i use XSSF, i am getting below Error.
java.lang.OutOfMemoryError: Java heap space at org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1592) at org.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1223) at org.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1144)
[Code]....
when i use HSSF , i am getting the below Error. java.lang.OutOfMemoryError: Java heap space
I have tried increasing the java heap size , by giving upto -Xms1500m -Xmx2048m
When there is a out-of-memory exception a heap dump is automatically generated by the JVM. But it seems that the heap dump permisions are set to read-write for owner only (600).
When I create files then they have by default read-write for owner and group/all read access (644).
$ ls -l total 795432 -rw------- 1 bamboop bamboo 811322265 Apr 14 09:18 java_pid337.hprof -rw-r--r-- 1 bamboop bamboo 6 Apr 14 12:57 test
Here the umask set for the server running the Java processes
I need to process 10000 xml files and verify and insert the data into database. I am loading all the files in the file object and iterating one by one. I am getting the memory issue. How to handle this?
Ok, so I'm making a game with a space ship that flies around the universe and discovering new planets. It works fine so far, but I'm looking to make it perform better and be better compatible with lesser-processors.So, I'm trying to put in an FPS counter and an entirely new game loop so that my game can decide for me what FPS I should use.
I have two classes. Heres the big, main one: [Java] package cyentw.game.src; import java.awt.Color; import java.awt.Font; impor - Pastebin.com *I want to change the loop and put an FPS counter in around line 456, you can scroll past the rest if you'd like*And heres the init frame one, in case you'd like to see it for some reason.
Java Code:
package cyentw.game.src; import javax.swing.JFrame; public class Start extends JFrame{ public static JFrame frame; public static int WIDTH = 500; public static int HEIGHT = 500; public Start() {
[code]....
how to make my game loop as quickly (or a bit slower) as it can, and my FPS is static.
I have been trying to learn how to use the TabbedPane GUI. I can get the tabs to show up, but the buttons I have placed in each tab do not show up. Why this is not working. I assume that, for some reason, the buttons are not linking with their respective panels, or the panels are not linking to the respective tabs.
public class Someone { String name; int age; ArrayList <Someone> listarr = new ArrayList <Someone>(); public Someone(String name1, int age1) { name = name1; age = age1;
[code]...
1. In the compare method, what happens when it returns one of the 0, -1, or 1? How does returning such values contribute to the sorting process? How does it take in information (like I know it does through the parameters, but how exactly)?
I am supposed to implement a custom Map interface and I'm having some trouble with this method:
// 1. From the interface /** * Gives an iterator iterating over the key set from the smallest key that is not less than the key provided. * @param key the key * @return the iterator * @throws NullPointerException if key == null */
public Iterator<Key> tailIterator(Key key);
[Code] .....
My implementation is wrong according to a JUnit test. Also, to get a full score this method should have a worst case running time of O(log N), which obviously isn't the case now. (My Map implementation is currently based on binary search in an ordered array, keeping a prallel array for the values).
I have always wanted to program A*, Breath First Search and Depth First Search ever since I took AI. I will only post BFS at the moment but it is only searching the row.
Java Code:
//Breath First Search. The first node passed to it is the start button void bfs(Mybuttons button){ Queue<Mybuttons> q = new LinkedList<Mybuttons>(); q.add(button); button.setIcon(button.setButtonToBlueWhileSearched()); button.model.visted=true;
[Code] ....
Output:
Java Code: In while loop In get Child Node In get Child Node In get Child Node In get Child Node In while loop In get Child Node In get Child Node In get Child Node In while loop In get Child Node In get Child Node In while loop In get Child Node mh_sh_highlight_all('java');
Say you have a bunch of animals, `Rat` `Cat` `Dog` and `Fish`, and you want to serialize/deserialize these animals. Each has its own `serialize()` method. That all works fine and good, if you want to save your Cat to a file, you call `Cat.serialize()` and then you write it to a file.
But what about deserializing? Sure, you could save an extra bit of data that states the type of animal, then use reflection to get a static method (say, `deserialize(String serialized)`) and then invoke it. But that's not safe. What if someone creates a new animal 'Rabbit' and doesn't include a `deserialize(String serialized)` method? Sure, you could then just catch the error and ignore the issue, but that seems unclean and bad practice.
You could also create an enum. Each item in the enum must have a `deserialize(String serialized)` method, and the aforementioned piece of data that states the type references the name of its enum item. Problem is, not very adaptable. Enums don't allow for other animals to be added at runtime.
The way I have been solving this issue is mapping the name of the objects against a deserializer object. Basically each animal would have to 'register' itself by entering it's name and deserializer (implements `Deserializer` interface) object into a HashMap. The deserializer object can then be retrieved via the name of the animal at a later time.
My question is, what is the best method to go about implementing deserialization? Also, is the method I have been using good/bad practice?
Finally, if you are wondering why animals would be added at runtime, the reason is because animals might be added by other non accessible parts of the program, or by things such as plugins.
Example of the method I have been using is below.
Animal Interface
Java Code:
public class Animal{ public String serialize(); } mh_sh_highlight_all('java'); Deserializer Interface
[code]....
And when you needed to deserialize:
Java Code:
Deserializer d=AnimalSpecies.getSpeciesDeserializer(serialized.split(AnimalSpecies.DELIMITER)[0]); if (d!=null){ d.deserialize(serialized); } mh_sh_highlight_all('java');
What I mean by this is that any serialized Animal must have a way of identifying which animal it is so that it's deserializer can be accessed. This can be worked around by implementing a wrapper that adds this information directly before it is written to a file, and removes it directly before deserialization.
Why not use built in serialization/deserialization? I would like the serialized data to be readable and easily editable.