Win a copy of Securing DevOps this week in the Security forum!

David Simkulette

Ranch Hand
+ Follow
since Nov 24, 2017
I cannot think of a single interesting thing about myself.
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
6
Received in last 30 days
0
Total given
4
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by David Simkulette

James Kilgarriff wrote:My problem is assigning the AI player at random. At the



Generally if you need to randomly (as if by statistical chance) do something in any program the approach is to use the Random class.

It is worth reading the javadoc.

The parameter to Random.nextInt defines the EXCLUSIVE value for the upper bounds on the int which is returned. The lower bounds are always zero and INCLUSIVE.

This will generate a random value and then you will write a switch or if-then-else  that creates one or the other class, depending on the value returned.

4 months ago

Ted Gress wrote:? Anybody have any ideas
why detach() and removecontent() might not work?



I doubt this is your problem. The methods seem to work.
OK so this is new news. Your original problem which was producing this error has gone away, correct?

In your OP you had an mystery Exception. Then in the recent post you said:

Ted Gress wrote:But it appears that the node is not being detached or the content is not being removed.



So if your board position has been advanced, let me know so I am addressing the current issue  

Actually, I can't speak for anyone else but my impuilse would be, since the Exception I opened with in the first post is no longer being thrown , then this is a new question.

The reason I suggest that is so people in the future, looking for answers to their specific questions, can recognize their questions being asked in a combination of the title of your post + your first question.

Basically, one problem down and now onto another, different problem, albeit in the same code. I know you still feel like you haven't had your problem solved, but you've had one of the problems in your code solved. Now you know you have others . *Sigh*. This is our lot as programmers ...LOL....

Do you think it might help if this were a new, fresh  thread so all participants can sync and focus clearly on the new issue?

What I hear you saying is: despite calling detach, and with no Exception being thrown anymore, one Node which should be being removed is not being removed. And this is happening between two JDom Documents
which are local (to a method ?). You cited one specific Node as being transferred when it's not.

Before I think any more about the problem, I need to  know if I understand the problem correctly or not.
Two word:

Read.

Books.

This is the way to advance yourself. There are a metric ton of books written in java explaining algos and data strcutures. Got to Amazon. Read the reviews, good and bad. That will help yuou sort through the pile.

Some truly good ones, classics, have high ratings but are written by experts for experts. That's not you. Look for ones where reviewers explicitly say they knew nothing and it taught them a ton.  Look for ones that appear to be geared towards the smart beginner- that's defintely you.

These are both errors invoving generics. The first one is telling you that you are using a raw type (no generics) where a generics type <SomeType> is expected. The second one is teling you that the method getListResult is returning a non-parameterized (non-genericized) List but it is being assigned to a List<DbResult> - type object.

The java tutorial on oracle's site has a good beginner's intro to generics that will get you past these issues.

It's not an eclipse thing.
4 months ago
This works for what you're trying to do. Notice the successful call to detach() and the subsequent use of the detached root node as the root of a new tree.

If detach is not working for you then I saw something in the javadoc that says subclasses have to called super.detach() in their detach method.

OK I have defintely contributed to your confusion by not considering all the possible contexts my answers could be considered in. So please, let me clean up my mess by being as concise as I can.

volatile-

1) can only be applied to variables, not classes or methods.

2) guarantees that no other thread will see a stale value of the variable; This means when Thread A *finishes* writiing to it, Thread B will see that finished-writing value, however.....

3) ... does not ensure atomicity UNLESS the variable itself has inherently atomic writes which is its most popular use, as far as I have seen.

Single atromic reads and writes are guaranteed (but NOT atomic incrementing, which is a read followed by a write, thus 2 operations) if the variable is one of:

byte,
boolean,
char,
int, 
float,

but NOT 64 bit longs or doubles, (but apparently yes! to their 32-bit versions! see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.7 )

No Object has inherently atomic reads and writes.

So in other words volatile does not, on it's own, provide atomicity but as a practical matter is often used with one of the above mentioned primitives and that combination does form an
{ atomic+no_stale_value_seen}  type contract, which is equivalent to synchronization.


Basically, if you think about your CPU it ( probably ) is actually 4 or more distinct CPUs, each with it's own L1 and L2 caches. So if the variable is cached in L1 or L2 cache on CPU #1 then CPU#4 isn't necessarily going to see it in main memory because the changes are happening on CPU#1's  L2 cache. 

That's the whole point of cache- save yourself from having to write to main memory.

So this is great for spped of execution but doesn't serve us so well when our programs are trying to share a value between those CPUs or between a single CPU and its caches and main memory.

So volatile solves the problem of visibility between all these places a variable's value could be hanging out, changing value. but it doesn't solve the problem of atomicity unless that atomicity is already guaranteed by the data type itself.



4 months ago

Mark Richardson wrote:
My motivation is probably a little naive... but I get excited about these YouTube videos talking about interviewing at places like Google/Amazon,etc. and being able to conquer Big-O notation problems.

My thinking is that as I progress in my career, and start to make more design/architectural decisions, this knowledge will come in handy.



Yeah 99% of the 1.5 % of the general population that programs in some langauge does not care this much so you're already head and shoulders above your peers in terms of attitude and foresight. You'll go far.
4 months ago

Praggi Gen wrote: Should I learn both (using Collection and making them by myself ?)



Heck yes. Companies want people who are "capable" of coding (something as complicated as) data structures. Everyone in comp. sci.  coded data structures from scratch, even though of course they already exist. We all did it.

Praggi Gen wrote: Q2) Will interviewer ask me to make Data Structure (my implementation not using the Collection ) ?



Who knows? They could. They might not. They could ask you for something even harder. Depends on the job. A lot of companies will just look at your past employment history, read your resume and hire you. Who knows? Do you want to chance it?

Praggi Gen wrote: Q3) For company like google do I need to make these by myself or can I use the Collections ?



Oh for Google they definitely want people who are capable of data structure design and more. No telling if the person you sit down with will ask you to do one, but it's immaterial because if you can't in principal create them, then you're not going to hold the job long anyway. Look up "google interviews" to see the craziness that goes on there.

Praggi Gen wrote: Q4) Do I need to know about the inner working of Collection classes ?


In general, for each category of DS you should have at some point actually written your own because that is the minimal definition of being a competent programmer, by unanimous agreement- understanding how to code things that complicated.



Praggi Gen wrote: I have been confused with this for like 2 month please help me out...



OK well two months is not a long time to have been coding. If you can code but data structures seem intimidating to you,as they do to most people,  then you need to find a good book on data structures and work through it as though you were in class in school and you cared about your grade. This is defintiely doable; people of just ordinary intelligence do it. Programming is not an IQ competition, but you have to sit down day after day and go through the material and learn it. For that you need a good book you can actually learn from (as opposed to a good book that is merely very sophsiticated and praised as "authoritative").


You are definitely smart enough to do it. CS majors take two semesters; one on elementary DS then one on advanced DS.

Find a book on DS written in a language you really know . The choice of book is paramount and be prepared to quit books that are too hard to understand (for you as you are today).

Everyone starts where you are right now. Then they do the work. You can do the work too. There is no royal road to data structures.

4 months ago

Ted Gress wrote:*That's* the part I'm having trouble with. I tried the detach and removecontent methods without much luck.



OK I see. What happens when you try to detach it? Do you get an error (other than the one you get when you try to attach it to the new tree..)?

Depending on how the API you're using (JDoms I mean) maybe they won't let you detach the root. Who knows? If you can't detach it somehow, then that means it's still attached , yes? If that's the case then you can't attach it (to the new tree (we believe that's what the error yo're getting is telling you) and therefore you can't move forward doing this.

In that case I think you're going to have to use a copy constructor on a per node basis to transfer the information from one tree to the next. This would be a whole tree iteration.

This is not that radical a notion; trees are stored (serialized) as XML and then turned back into trees at runtime by reading the xml the tree was written to and creating all new nodes from that information.

But before you go there, spend some time nosing around inside the API to see if there's a single method call to do just what you're trying to do (tree. clone()) or node.copy() or util.Transfer(node1,node2)  or something like this.

Besides, what DOES detach do if not, you know, detach??

But right now- the first part of my question, is there an error message invovled?

Ted Gress wrote:
Object object = new Object and then casting object to a string doesn't work even though String is still a subclass of Object



It's what everyone is telling you. See my first post again:

David Simkulette wrote:You cannot cause one type object to become another type merely by casting. That is what your program does.  

4 months ago

Ted Gress wrote:So - somewhat related question. Is there any way to "pool" memory in Java?



In Java, memory is always created by the VM and destroyed by the garbage collector in increments of the amount required to hold the state of some specific object(s). To get memory, you create an object (r-value) and to release memory, you let all the l-values pointing to it go out of scope or set them to null. When there are zero l-values pointing to an r-value, that r-value is eligible for garbage collection, meaning that the memory dedicated to holding its state will be marked as unallocated memory, available to hold some other object's state.

The garbage collector affectionally known as GC will get around to marking it thus as it sees fit and on its own schedule.

You cannot directly access "memory" to "pool" it  as such (except see below).

The closest things Java has to this are

1) pre-creating objects of some type before you actually need them and maintaining active references to those objects. (active l-values referencing r-values).
2) defining an Collection or array to hold a certain number of objects of some type. The VM will allocate enough memory for that many objects beofre those Collection items or array cells are ever assigned any r-values.


Exception, sort of. You can command the VM to start with a certain minimal amount of memory dedicated to it and you can also tell it to use some up to maximal amount. These are he VM arguments   -Xms and  -Xmx respectively.
4 months ago

Ted Gress wrote:Would it be possible to do:

Object object = new String(); since String is a subclass of Object?



Sure. For just the reason you said. The resulting object would be casatable to a String and assignable to a String-and-its-superclasses-typed L-value, thus :

String string =(Stirng) object;

But without casting or reassigning to such an L-value, no method whose formal parameters call for a String is going to accept it as a String to a method invocation. 

It's great to clearly get these concepts clear in your mind so you don't always have to test things out on the compiler, (and can answer test questions !) but also, learning to see the compiler as a LittleLab where you can ask just these sorts of questions as hypotheses and get them confirmed and disconfirmed is a powerful habit of mind to develop and will give you a kind of programming intellectual independence later when your programming  questions get tougher nad no one actually knows the answer anymore.
4 months ago

The Big Three are IntelliJ Netbeans and Eclipse


...and IntelliJ is the best.


*ducks*
-
4 months ago
Just the evaluation of the regexp itself floored your CPU for a long time? Not the code around it? Was it in a loop? Did you get a stack overflow?

Huh. Under the hood, regexps do iterate I think. Can you share the evil regexp with us?
4 months ago