Jj Roberts

+ Follow
since Oct 25, 2019
Jj likes ...
Firefox Browser MySQL Database Java Ubuntu
I dabbled in writing C code for micro controllers for a bit. Around June 2019 I was introduced to Java. I loved it from the start. Now I am studying for the Oracle Certified Professional exams.
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt Green check

Recent posts by Jj Roberts

My pleasure. That piece of code was just an example of how your method could be made more concise. Do you understand all of it? I know the many new programmers get confused by the ternary statement, which I used on line 4. It would also be helpful to you to work through the advice and suggestions that other people posted (like looking at using arraycopy), and see how you can improve your code further. Have fun!
12 hours ago
It works for me as you have provided it; this is how I tested it:I think it would be a good idea to pass an array into the method as a parameter, and return the new array rather than modifying your class's field in the method. Also, you want to avoid having to repeated pieces of code which are almost identical. If we ignore everything else for now, we could shrink your method to
1 day ago
Do  you want to remove to middle number of the array if it contains an odd number of elements, and remove the two middle numbers if there is an even number of elements? If that is what you want, your removeMiddle() method seems to be working. I don't see it called in your main method, though. What is the problem you are having? Does it give an error when compiling or running, or are you getting a wrong result?

1 day ago

Campbell Ritchie wrote:
Did you read the documentation for List#toArray()? Did you see what its return type is? Did you see the strange design of its overloaded brother? For an exercise, see if you can find a version of that overloaded method for Java1.2 or Java1.3 or Java1.4 and see how it differs from the current version.

Henry noted that toArray(new String[0]) returns a String[], and that toArray() returns an Object[]

Is  down casting allow here? String[] array2 = (String[]) list.toArray(); This returns an Object array.
I know that String[] array2 = list.toArray(new String[0]) works by specifying the String[] array as a parameter for toArray() method. And it will give you a String array.

I think the root of the confusion lies in what the cast actually does, in widening vs. narrowing contexts.
1 day ago
From the Java Language Specification 10.2:

If an array variable v has type A[] , where A is a reference type, then v can hold
a reference to an instance of any array type B[] , provided B can be assigned to A
(§5.2). This may result in a run-time exception on a later assignment; see §10.5
for a discussion.

The issue is, as you pointed out, that your objectArray variable holds a reference to an object of the Object[] type. An Object[] object cannot be assigned to a variable of type String[], hence your exception. When you create a new String array, however, it is a String[] object. A variable of type Object[] can hold its reference, because String is assignable to Object. Casting the reference type to String[] is fine, because that is what it is. There is no problem assigning a String[] object to a variable of type String[], obviously   .

It is the same with your last example. There is no problem assigning an String[] object to a variable of type Object[], since String is assignable to Object
1 day ago
Congratulations! Enjoy your new title, Zachary!
1 day ago
One of the topics in the OCP 11 is parallel streams:

Parallel streams
   • Develop the code that use parallel stream
   • Implement decomposition and reduction with stream

What is decomposition in the context of parallel streams? I have tried to find out, but have not come up with anything. Is is talking about decomposing data with Spliterator?
Welcome to the Ranch!

Do you want all the threads of each batch to exit before scheduling a new batch? I'm no expert on concurrency, but CyclicBarrier sounds useful for that. You could have the finished threads call the barrier's await() method, which will block until a set number of threads have called it.

First of all, don't use ternary statements if you're going to return booleans from them...

Yes, that was a bad example  

Your alternative approach is interesting. Would you do this normally? I agree that is very self-documenting. It is clear what is happening without writing a specific method for validating the row number, though, isn't it? Also with the similar method for getting the column from the matrix, it becomes six methods instead of two (which may or may not be a bad thing).

Your other points are good, thank you.
1 week ago
I know well that methods which follow the pattern "if x, return a, else return b" should be written with a ternary, which is obviously very clean:All is well so far. I sometimes find myself with the same pattern, but with too much logic to fit into a ternary. In that case I often find myself writing something like this:
I tend to get the bad cases out of the way, then write the code as if they did not exist. The logic is actually still if-else, though, so I wondered if I should rather writeI favour the first; to me it  looks cleaner. I know that as a fairly green programmer my opinion doesn't count for much. What should I be doing?
1 week ago
A clue. If you went and had a look at the println() method of the PrintStream class (System.out is a PrintStream) I think you will find your answer.  
1 week ago
They are constant values, once they are created they cannot be changed

From Java Tutorials — Enum Types:

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

Later on:

Each enum constant is declared with values for the mass and radius parameters. These values are passed to the constructor when the constant is created. Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon.

Your teacher is correct, they are referred to as enum constants. It does make sense, because enums are the datatype made to represent a fixed set of constants.
I hope that helps  

Edit: note that your example of an enum is an example on the page I linked to; I think it probably comes from there.
1 week ago
2 weeks ago
It seems that the way you are doing it now is pretty much how everyone has to do it in core Java. However looking at Baeldung - Converting a Stack Trace to a String in Java you might want to have a look at the getStackTrace() method from the Apache Commons Lang class ExceptionUtils.
2 weeks ago

Junilu Lacar wrote: ...
A reference is a "handle" to an object. It's a way to access an object and its ...

A much better way to describe it  
2 weeks ago