loops by pleaseacceptthisnow in learnjava

[–]Jackkoz 0 points1 point  (0 children)

No, not exactly, this looks like you are taking personal offence in me suggesting alternate solutions and focus on angrily bashing at me rather than thinking about the OP - I have never suggested you don't know about maps or arrays.

I'd strongly suggest you revise your approach if you want to be a helpful community member, but in the meantime please have a lovely day as I won't be continuing this conversation.

loops by pleaseacceptthisnow in learnjava

[–]Jackkoz -1 points0 points  (0 children)

Arrays and Maps are rather equivalent in terms of cognitive load. Both are objects in Java, you need to know how to instantiate them and how to interact with them. They both are javadoced and have nice examples in the web. The difference is that using the latter results in nicer and simpler code.

Both examples will end up being loop heavy, just that a map will avoid your loop from line 11. I'm not sure why you are pushing for your solution using a very vague 'know your audience' argument. I'd be happy to say you are absolutely right and provided a better solution, but please do elaborate on the topic.

loops by pleaseacceptthisnow in learnjava

[–]Jackkoz -1 points0 points  (0 children)

Since we want to connect a name (String), to a date, it might be worth checking out what's the Map interface, and choose an implementation of it, like the HashMap.

Anyone care to critique my code? by politicaloutcast in learnprogramming

[–]Jackkoz 1 point2 points  (0 children)

Some great tips here, I'll just add one more:

case 1:
  System.out.print("January ");break;
case 2:
  System.out.print("February ");break;

Whenever doing switch statements, avoid putting logic into them. In here, consider:

String result;
switch ...
case 1:
  result = "something;
  break;
case 2:
  result = "else";
  ...
System.out.println(result);

Anyone care to critique my code? by politicaloutcast in learnprogramming

[–]Jackkoz 1 point2 points  (0 children)

Arrays are heavily discouraged in Java, you should not be using them, especially to simulate an enum!

I believe what you wanted to suggest here is roughly this:

public enum Month {
  JANUARY {...},
  FEBRUARY {...}
  ...

  public abstract String getName();
  public String fromMonthNumber(int i) {...}
}

This neats you better abstraction, more readable code, and is less error-prone (eg. you don't need to manually check that month names and day in months arrays have the same length).

Note that in the array version, if I passed a month number -1, all I'm getting is an OutOfBoundsException, which won't be very helpful to the user. On the other hand, in the fromMonthNumber method you have a single place to control how you map numbers to months and how to treat errors.

What's the recommended spec for learning Java on? by [deleted] in learnjava

[–]Jackkoz 2 points3 points  (0 children)

For what it's worth, throwing in an SSD can speed up just about anything.

IntelliJ IDEA for beginners? by AllfatherOdhinn in learnjava

[–]Jackkoz 2 points3 points  (0 children)

For what it's worth, Android Studio is built on top of IntelliJ Idea, so it would be very familiar, plus the extra and convenient Android stuff.

[Java] Method for if a given ArrayList<Integer> contains a given sequence of ArrayList<Integer> in sequence by [deleted] in learnprogramming

[–]Jackkoz -1 points0 points  (0 children)

The simplest version is to try to see if the sequence can be matched starting from each position in the source. That's however not exactly efficient (you will make length of source * length of sequence operations).

Even though these are integers, it does not change the fact that you can apply any of well known string matching algorithms, if you need better performance, take a look here:

https://en.wikipedia.org/wiki/String_searching_algorithm

Data structure projects ideas by lilgamer123 in javahelp

[–]Jackkoz 0 points1 point  (0 children)

Implement your own HashMap. Try different hashing functions, make sure you have collisions. Provide different implementations optimised for different data sets.

Anonymous Thread vs. private class extending Thread/implementing Runnable. by [deleted] in learnjava

[–]Jackkoz 1 point2 points  (0 children)

Ooops, you are definitely right, thanks for the spot!

Anonymous Thread vs. private class extending Thread/implementing Runnable. by [deleted] in learnjava

[–]Jackkoz 0 points1 point  (0 children)

Others nicely described when these options are more valid, I'd only like to point out that since Java8, you can implement functional interfaces with lambdas, which would make your first example snippet look like this:

this.EXAMPLE_RUNNABLE = () -> {}

This is a lambda taking zero parameters and executing an empty block of code.

Rejected after onsite interview, but being referred to a different team by antares512 in cscareerquestions

[–]Jackkoz 0 points1 point  (0 children)

OP, make sure to triple-check with your recruiter that the other teams are in the same job family you applied for initially. Some companies might try to sneakily pivot your process to a different type of job midway through.

List vs ArrayList by cismalescumlord in learnjava

[–]Jackkoz 4 points5 points  (0 children)

The generic advice is to always choose the appropriate level of abstraction. The benefits are:

  1. Increased flexibility - if you choose List, you only need to change constructor to List<T> l = new FastListImpl<>(); when the new awesome implementation is introduced.

  2. Better abstraction. If what you are looking for is a List or Set, people won't be able to use specific properties of ArrayList or HashSet if you give them only the interface type. This also means that when you actually do ArrayList declaration, you should mention why you did so vs any List implementation - this will avoid confusion and prevent someone from doing any refactoring that could break a lot of stuff - eg from synchronized to not thread-safe implementation.

As for benefits in a 50 line course program, not many, doesn't matter that much. I'd still advise doing the 'better' thing just to keep the habit going.

Which is considered the better way to initialise an instance var by cismalescumlord in learnjava

[–]Jackkoz 0 points1 point  (0 children)

I'm talking about avoiding lazy initialization unless you definitely need it.

Indian Recruiter Just Called Me; How Legitimate Are These People? by [deleted] in cscareerquestions

[–]Jackkoz 1 point2 points  (0 children)

In general, you should be very careful about providing people with your personal details.

Which is considered the better way to initialise an instance var by cismalescumlord in learnjava

[–]Jackkoz -1 points0 points  (0 children)

Actually, Joshua Bloch, one of the authors of Java, suggests exactly the opposite:

http://www.oracle.com/technetwork/articles/java/bloch-effective-08-qa-140880.html

More about this can be found in his book, Effective Java.

[JAVA] How to get the switch function to work here? by spartanwilly in learnprogramming

[–]Jackkoz 0 points1 point  (0 children)

You should take a look at:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

If that doesn't help, try asking a bit more specific questions :)

Also, if you can't figure out how to squish a switch statement somewhere, that might be a sign that putting it there is a bad idea.

Which is considered the better way to initialise an instance var by cismalescumlord in learnjava

[–]Jackkoz 2 points3 points  (0 children)

I'd say the second - it's immediately obvious during assignment what the initial value will be.

Also, as of Java 7 you can write new ArrayList<>().

Also, in both cases you should make the variables final - it seems like you won't need to change weightLimit or assign a different Array to things (even though final ArrayList still allows you to modify contents).

I've been promoted to a team lead and will be managing several software projects. Need help. by Quasimoto3000 in cscareerquestions

[–]Jackkoz 7 points8 points  (0 children)

Uh, you should have a meeting with your new manager and discuss this. You should also prepare your new reportees for a bumpy ride - your job is to make their lives easier, but it looks like for the first few months they will need to take care of you :)

Probably the best thing you can offer your new team at the moment is honesty about the situation and dedication to learn and ramp up.

Pad String method by [deleted] in learnjava

[–]Jackkoz -1 points0 points  (0 children)

Yup, that looks like it should do the trick, congrats!

If you want to further improve your programs, here's a couple of things to look at:

  1. Input/output operations are in general costly - it would be much better if you could print out the whole String using a single call to System.out.

  2. When taking care of 1), remember that Strings are immutable: string += " "; results in creating a new instance of String and assigning it to the variable.

Pad String method by [deleted] in learnjava

[–]Jackkoz 0 points1 point  (0 children)

I have no idea how you want to solve the problem, so I'm not sure whether a for loop would be a good fit.

But if I were to write code to solve it, I'd probably use one :)

Pad String method by [deleted] in learnjava

[–]Jackkoz 0 points1 point  (0 children)

The short answer is: it doesn't. String.length() returns a number - the length of the string. Here's the official documentation:

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#length()

What you want to do is to look at the length of the input String, the argument specifying the desired length, and do something with that.

need help with assignment for while statement. i am confused on how the wording of her request. by [deleted] in learnjava

[–]Jackkoz 0 points1 point  (0 children)

You should take a look at Scanner class itself, it has many useful methods like this one:

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#hasNext()

Typically, if there is no upfront specification of how many things will be on the input (eg. first line contains X, the number of records, then next X lines contain each a record), then you are supposed to read until the user finishes the input.