Browsing 7239 questions and answers with Jon Skeet

Get substring with only available chars from index

Is there any string function in Java that will allow me to extract the substring like QString::mid: http://doc.qt.io/qt-5/qstring.html#mid When index exceeds string length, I...
Jon Skeet
people
quotationmark

It sounds like you just need Math.min a couple of times: int start = Math.min(index, s.length()); int end = Math.min(index + length, s.length()); return s.substring(start, end); more 4/12/2015 8:13:30 PM

people

Store a list of unordered pairs in java

Say I have the following ordered pairs (A, B), (A, C), (B, C) and (B, A). Out of those 4 pairs, there are 3 unordered pairs. My question is: Does java have a data structure...
Jon Skeet
people
quotationmark

Does java have a data structure that does NOT allow duplicates or palindromes? Well Java has a number of sets - HashSet, for example. Those don't allow multiple equal values to be stored... so you just need to create a class which... more 4/12/2015 2:14:46 PM

people

INSTANCE in a Java Enum

It is a good practices to define INSTANCE in an Enum, for the benefit of serialization for free (Effective Java Edition 2, Item 3). If anyone could explain a bit more what it...
Jon Skeet
people
quotationmark

Here's a demonstration: import java.io.*; class ClassSingleton implements Serializable { public static final ClassSingleton INSTANCE = new ClassSingleton(); private ClassSingleton() {} } enum EnumSingleton { ... more 4/12/2015 6:40:23 AM

people

are static and non static overloads each other

Does these two function overloads class yogi{ public static void fun(){ System.out.println("Fun"); } public void fun(int a,int b){ ...
Jon Skeet
people
quotationmark

Yes, those are overloads. From JLS 8.4.9: If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not... more 4/11/2015 6:28:03 PM

people

Simple short addition yields warning

I found some questions concerning short arithmetic but none of them compared the following three cases. I wonder why these two pieces of code are ok (a) short m =...
Jon Skeet
people
quotationmark

It's not a warning - it's an error. There are two facts at work here: There's no short + short operator; the "smallest" addition is int + int, with a result type of int, and the operands are automatically promoted to int if necessary... more 4/11/2015 2:02:58 PM

people

Get all .net available type in system namespace

Is there a function or by using reflection a way to get all the System types. Like those: - System.Int64 System.Byte[] System.Boolean System.String System.Decimal System.Double ... We have an old enum that stores some datatype. We need to convert those to .net types.
Jon Skeet
people
quotationmark

Assuming you only want types from mscorlib, it's easy: var mscorlib = typeof(string).Assembly; var types = mscorlib.GetTypes() .Where(t => t.Namespace == "System"); However, that won't return byte[], as that's an... more 4/9/2015 5:44:52 PM

people

Convert LINQ method syntax with method withing .Select() to LINQ query syntax

I currently have the following code: dataSource.Where(row => row.Value == 1) .Select(row => {row["Text"] = translatedText; return row;}) .Single(); So...
Jon Skeet
people
quotationmark

Is there a possibility to translate this method chain to LINQ? (By "to LINQ" I believe you mean "to query expression syntax".) Not directly, no. Effectively, you can only convert expression-bodied lambda expressions into query... more 4/9/2015 3:03:35 PM

people

Out of memory exception while playing with Monitor.Wait and Monitor.Pulse

I was playing with the Monitor class in .NET so I arrived at a piece of code that seem to be working but when I loop it for a while it throws an OutOfMemoryException. I am...
Jon Skeet
people
quotationmark

For each pair of threads, if thread A manages to acquire the lock before thread B, you'll end up with both threads completing, and everything can be cleaned up. If thread B manages to acquire the lock before thread A, thread B will... more 4/9/2015 2:47:03 PM

people

Removing elements from List containing null using Iterator

List<Integer> list = new ArrayList<>(); list.add(12); list.add(null); list.add(22); list.add(32); System.out.println(list); Iterator<Integer> iterator =...
Jon Skeet
people
quotationmark

You absolutely can remove from a list containing a null reference - you just can't call equals on a null reference. The problem is here: if(a.equals(32)) That will fail with a NullPointerException if a is null. There's nothing... more 4/9/2015 2:25:25 PM

people

Problems with throwing exception

the method public static List<Signal> fromString(String inps) create and returns the List of Signal values found in the input string. If any characters other than those in...
Jon Skeet
people
quotationmark

Look at this condition: if (inps.charAt(i) != ' ' || inps.charAt(i) != '\t') That will pass if the character isn't space or if it isn't tab. A single character can't be both space and tab, so at least one of the subconditions will be... more 4/9/2015 6:50:50 AM

people