Get only part of an Array in Java?
The length of an array in Java is immutable. So, you need to copy the desired part into a new array.
Use copyOfRange
method from java.util.Arrays class:
int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex);
startIndex is the initial index of the range to be copied, inclusive.
endIndex is the final index of the range to be copied, exclusive. (This index may lie outside the array)
E.g.:
//index 0 1 2 3 4
int[] arr = {10, 20, 30, 40, 50};
Arrays.copyOfRange(arr, 0, 2); // returns {10, 20}
Arrays.copyOfRange(arr, 1, 4); // returns {20, 30, 40}
Arrays.copyOfRange(arr, 2, arr.length); // returns {30, 40, 50} (length = 5)
get a view of the portion of Java array
Refer to this answer from the same question that you referenced. Wrap the array with Arrays.asList() and use List.subList():
Integer[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
List<Integer> view = Arrays.asList(a).subList(3, 6);
for (int i = 0; i < view.size(); i++)
view.set(i, view.get(i) * 10);
System.out.println(view);
System.out.println(Arrays.toString(a));
prints:
[30, 40, 50]
[0, 1, 2, 30, 40, 50, 6, 7, 8, 9]
However, you won't be able to wrap arrays of primitive types without boxing the whole array first.
Initialize part of an array in java
You can do something like this, it will create array with new size based on provided.
String[] temp = new String[] {"water", "shovel", "berries", "stick", "stone", "seed", "axe"};
String[] val = Arrays.copyOf(temp, 20);
System.out.println(val.length);
System.out.println(Arrays.toString(val));
The output will be:
20
[water, shovel, berries, stick, stone, seed, axe, null, null, null, null, null, null, null, null, null, null, null, null, null]
Java copy section of array
Here's a java 1.4 compatible 1.5-liner:
int[] array = { 1, 2, 3, 4, 5 };
int size = 3;
int[] part = new int[size];
System.arraycopy(array, 0, part, 0, size);
You could do this in one line, but you wouldn't have a reference to the result.
To make a one-liner, you could refactor this into a method:
private static int[] partArray(int[] array, int size) {
int[] part = new int[size];
System.arraycopy(array, 0, part, 0, size);
return part;
}
then call like this:
int[] part = partArray(array, 3);
How to print part of a String from an array (Java)?
Below code is exactly what you are looking for (i guess)
String[] guests = { "Rock Adam",
"Stewart Thomas",
"Anderson Michael",
};
List<String> emailIdList = new ArrayList<>();
for (String guest : guests) {
String firstName = guest.split(" ")[1];
String lastName = guest.split(" ")[0];
String emailId = firstName.substring(0,2) + lastName.substring(0,1) + "@guest.com";
emailIdList.add(emailId);
}
Java: is there an easy way to select a subset of an array?
Use copyOfRange
, available since Java 1.6:
Arrays.copyOfRange(array, 1, array.length);
Alternatives include:
ArrayUtils.subarray(array, 1, array.length)
from Apache commons-langSystem.arraycopy(...)
- rather unfriendly with the long param list.
How do I print out only the first 3 elements of an array that has 5 elements in java?
Print first 25 elements:
for (int i=0; i<25; i++) {
System.out.println(strArray[i]);
}
Print element number 10 to 50:
for (int j=9; j<50; j++) {
System.out.println(strArray[j]);
}
Java - Slice any array at steps
If you are using Java 8, then you can make use of streams and do the following:
int [] a = new int [] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// filter out all indices that evenly divide 3
int [] sliceArr = IntStream.range(0, a.length).filter(i -> i % 3 == 0)
.map(i -> a[i]).toArray();
System.out.println(Arrays.toString(sliceArr));
Outputs:
[0, 3, 6, 9]
Related Topics
When Should I Use an Interface in Java
What Is the --Release Flag in the Java 9 Compiler
Where Do Java and .Net String Literals Reside
Is Java.Util.Date Using Timezone
What Is the Cross-Platform Way of Obtaining the Path to the Local Application Data Directory
Hiding Instance Variables of a Class
How to Get Data Between Quotes in Java
Instanceof - Incompatible Conditional Operand Types
Jsf Convertdatetime Renders the Previous Day
Java Garbage Collector - When Does It Collect
Java.Util.Zip.Zipexception: Error in Opening Zip File
How to Reference Another Property in Java.Util.Properties
Difference Between Year-Of-Era and Week-Based-Year
Problem with "Scopes" of Variables in Try Catch Blocks in Java