This question already has answers here:
Get only part of an Array in Java? [duplicate]
(8 answers)
Fastest way to get the first n elements of a List into an Array
(6 answers)
Closed 4 years ago.
I want to pick first N elements from a list in java
String[] ligature = new String{there,is not,a,single,person,who,knows,the,answer};
Now I want to pick first 5 elements from this.something like
Stiring[] selectedLigature = ligature[0:4];
I want to do it without using for loop.
Do not stream this simple case, there is subList for this:
// for a List
yourList.subList(0, 5)...
// for an array
Arrays.copyOfRange
Arrays class has a method for this:
Arrays.copyOfRange(ligature, 0, 5);
Related
This question already has answers here:
Remove elements from collection while iterating
(9 answers)
Closed 2 years ago.
I have an arraylist like this:
ArrayList<Integer> numbers = new ArrayList<Integer>();
And let's say the values inside are like:
[0,1,2,3,4,5]
So I'm looping through and I want to remove values from the arraylist while I'm looping through:
for(int i=0; i<numbers.size();i++){
if(numbers.get(i)>2){
numbers.remove(i);
}
}
How would I do this?
You can call numbers.remove(i) and then decrement i to follow the change in the position within the list: numbers.remove(i); i--;.
If you use iterators, foreach or the : operator you can't remove values, because they use unmodifiable lists.
If you have a simple way of determining whether a number should be removed, you can use numbers.removeIf((number)->number>2) (like the problem in your example).
This question already has answers here:
Java Arraylist remove multiple element by index
(8 answers)
Closed 2 years ago.
I have a List with some elements. My method get random element from List like
ListName.get(num)
How can I remove element from List with that num I've got?
Call List::remove and pass the same number.
myList.remove( num ) ;
listname.remove(elementnumber)
This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 5 years ago.
I have taken an int[] as input. For searching the index of an integer in the array I used Arrays.asList(arr).indexOf(element) method. However, I am getting index as -1 even if the element is present in the array.
int[] is one object, so Arrays.asList(arr) puts one object in the list, you need to put values from int[] one by one
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 5 years ago.
how to use toString() method in arrays bigger than one dimensional for example
int[][][] array = new array[x][y][z];
print one dimensional
Ststem.out.print(array[a][b].toString());
print all dimensional
System.out.print(array.toString());
my console only show :
[I#15db9742
You have a multidimentional array so you need to do:
Arrays.deepToString(array);
or nest for loops and print the element at index i,j,k
It looks like more like Java :).
In any case, I'd create a function that iterates through the different levels and print each element of the array.
This question already has answers here:
How to create a sub array from another array in Java?
(8 answers)
Closed 8 years ago.
I have one array, say
String[] a={a,b,c,d,e,f,g}
I want this array in second array but without the first element in the array, for example
String[] b{b,c,d,e,f,g}
How do I achieve this?
You can use System.arrayCopy() (jdk 1.5) or Arrays.copyOfRange() (jdk 1.6+) methods.