[Info-vax] Simple Pascal question
Arne Vajhøj
arne at vajhoej.dk
Thu Sep 5 13:30:47 EDT 2024
On 8/4/2024 9:00 PM, Arne Vajhøj wrote:
> Java does not have anything like slices.
Let me correct that.
Java 22 actually introduced something similar (started as
preview back in Java 14).
But it looks absolutely horrible!
import java.lang.foreign.MemorySegment;
import java.lang.foreign.ValueLayout;
public class SliceFun {
public static void dump(String lbl, MemorySegment ms) {
System.out.printf("%s : ", lbl);
for(int i = 0; i < ms.byteSize() /
ValueLayout.JAVA_INT.byteSize(); i++) {
System.out.printf(" %d",
ms.getAtIndex(ValueLayout.JAVA_INT, i));
}
System.out.println();
}
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5 };
MemorySegment ms = MemorySegment.ofArray(a);
dump("All", ms);
dump("Middle", ms.asSlice(1 * ValueLayout.JAVA_INT.byteSize(),
3 * ValueLayout.JAVA_INT.byteSize()));
}
}
Arne
More information about the Info-vax
mailing list