Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: [JLS] new long[0][ -1 ] Date: Fri, 10 Jul 2015 22:24:21 +0000 (UTC) Organization: A noiseless patient Spider Lines: 83 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 10 Jul 2015 22:24:21 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="17438"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OcjAnWpZLj2cUpln+cSQ0k+eS+E0midg=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:rhxsfB/0vY+BvHpIy42DtYM9f6I= Xref: csiph.com comp.lang.java.programmer:31988 On Fri, 10 Jul 2015 16:43:06 -0400, Eric Sosman wrote: > On 7/10/2015 4:19 PM, Martin Gregorie wrote: >> >> Two features I'd have liked to see in Java are non-zero based arrays, >> e.g. >> int temperatureOcurrences[-40:55] and flexible array bounds, e.g int >> samples[0:flex] > > Would you mind elaborating on the latter? Java already has > > int flex = ...; > int[] samples = new int[flex+1]; > > ... but do you mean something different? Do you mean, perhaps, > that a subsequent assignment to `flex' would grow or shrink the array? > Brief pause while I found my Algol68R User Guide (I haven't used the language since 1977. I mean the latter, for instance the declaration [1:0 flex] real a; declares an array with flexible bounds and initially no elements. So I can do the following: a := (1.0, 2.0, 3.0); and somewhat later, I might do this: a := (0.0, 1.1, 2.2, 3.1415926, 22.0/7.0, 0.95); Given that there are operators associated with arrays (to extract the lower and upper bounds (you specify which dimension for a multidimensional array, flexible (returns true if its a flexible array) and clear (sets all elements to the 'empty value' (0 if numeric, space if char, false of boolean) if you think of a flexible array as being similar to the String class you'd not be wrong. A68 was a sort of half-way house between 'classical' and OO languages: you could overload any operator, define new operators and do the same with data types (known as modes). For instance, you could declare a sketch mode to hold a set of plottable modes, derive things like rectangles, circles and scalable text from the sketch and overload the '+' operator so it could be used to add and these to another sketch, so you could write circle [] c = ( list of circles ); sketch s := frame + text("My Sketch"); for i from lwb c to upb c do s += c[i]; But A68 was NOT an OO language because there was no concept of a class and its instances. The most you could do was declare a program segment which would normally contain a set of mode definitions and the operators and/or procedures needed to use them. A segment was more like a C source module than a class declaration. as NOT an OO language > What output would > > int flex = 5; > int[] samples = new int[flex+1]; System.out.println (samples.length); > flex = 20; System.out.println(samples.length); > flex = -42; // NegativeArraySizeException? > System.out.println(samples.length); > You can't do that: once a flexible array is declared, you change its size by assigning stuff to it. IIRC you could do something like: int [] source = (1,2,3,4,5,6,7,8,9,0); [1:0 flex] a; // an empty flexible array; for i from lwb source to upb source do a[i] = source[i]; IOW a flexible array doesn't do much that an ArrayList can't do; It can be very useful when you're parsing lines of text and want split it into an array of words. Depending on what you want to do, though getting away from zero-based arrays is rather more useful, if only because it gets you away from continually calculating the base offset. HTH -- martin@ | Martin Gregorie gregorie. | Essex, UK org |