Is it possible in Java to make an array in a style similar to this, i have been searching for a while and haven't found anything.
int[] foo = {
for(String arg:args)
return Integer.parseInt(arg);
};
No, but you can do this instead :
int[] foo = new int[args.length];
for(int i = 0; i < foo.length; i++) {
foo[i] = Integer.parseInt(args[i]);
}
Kind of... Since Java 8 we have streams which can simulate loop and allow us to do things like
int[] arr = Arrays.stream(args).mapToInt(s -> Integer.parseInt(s)).toArray();
or its equivalent using method references
int[] arr = Arrays.stream(args).mapToInt(Integer::parseInt).toArray();
int[] foo = new int[arg.length];
for (int i =0;i<args.length;i++) foo[i]=Integer.parseInt(args[i]);
With array no, but you can do something similar with List:
final String args[] = {"123", "456", "789"};
List<Integer> list = new LinkedList<Integer>(){
{
for (String arg: args){
add(Integer.parseInt(arg));
}
}
};
System.out.println(list); // [123, 456, 789]
With array you have to do the following:
int[] foo = new int[args.length];
for (int i = 0; i < foo.length; i ++) {
foo[i] = Integer.parseInt(args[i]);
}