0

So I noticed something.

When doing a recursive method on codingbat, I looked at the:

String abc = "abc";
String mod = abc.substring(1);
System.out.println(mod); //prints "bc"

So I thought:

Hey, why not have a substring-like method for arrays?

For example:

String[] abc = {"a", "b", "c"};
String[] mod = abc[1, str.length];
for(int i = 0; i < mod.length; i++){
  System.out.print(mod[i]);
  if(i + 1 != mod.length){
    System.out.print(", ");
  }
}
// This would print out: "b, c"

So, as you can see from this, it is like the substring method as it adds the Objects from the first array to the second array from start index, to end index, but not including the end index (avoids OutOfBounds Exceptions).

How would this be going around to be made since I cannot seem to find the class that controls the "[ ]"'s since something has to regulate them because they aren't just "there" they had to be added in some way.

Thanks for any constructive criticism and feedback.

FirexRanger8

Matt
  • 95
  • 6

5 Answers5

2

How would this be going around to be made since I cannot seem to find the class that controls the "[ ]"'s since something has to regulate them because they aren't just "there" they had to be added in some way.

They are "just there" in that they're hard-coded as part of the language and platform.

If you want to change how arrays are handled by the language, you'll have to make a change to the Java compiler. If you want to change how they behave at execution time, you'll have to change the JVM...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • hmm, maybe I should send this to Java. It does have applications as it reduces excessive methods, and is easy for developers. Thanks for the answers. You might see this is JDK and JRE 6 update 30 ;D – Matt Oct 20 '11 at 22:43
  • @Firexranger8 Good luck with that. It's difficult enough getting stuff people actually *want* into Java. – Dave Newton Oct 20 '11 at 22:45
  • @Firexranger8: I think it somewhat unlikely. There are a lot of suggestions for language features, but this seems to be just an alternative way of creating a copy of part of an array... sounds like a fine thing for a *method* to do. – Jon Skeet Oct 20 '11 at 22:45
  • @Jon Skeet: And closures are just an alternate way of creating anonymous inner classes! A (hopefully obvious) joke; I agree with you... – Kevin Oct 20 '11 at 22:49
2

You can't overload operators like in c++, so achieving that exact syntax is not possible in java. But there already is a subList function exposed in List interface, maybe that is what you want?

If you're interested in doing something like this you should look at alternate JVM languages like scala or groovy, which have much more flexible syntax. For example, see:

Slice notation in scala?

Community
  • 1
  • 1
Kevin
  • 24,871
  • 19
  • 102
  • 158
2

They're part of the Java language spec and what they do can't be changed, in Java.

There are other languages (ie: Groovy) that target the JVM, have a language syntax similar to Java, and do support things like overriding operators such as array indexing.

But, it can't be done in Java.

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
2

I'm pretty sure this is a wheel that's already been invented:

String[] abc = { "a", "b", "c" };
String[] mod = Arrays.copyOfRange(abc, 1, abc.length); // now mod = [b, c]
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

The java.util.Arrays.copyOfRange function will do what you want ("a substring-like method for arrays") (just not with the syntax you want).

Boann
  • 48,794
  • 16
  • 117
  • 146