Removing elements from arrays? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I saw this snippet of code in a forum with the same question, however I needed 50 reputation to comment on the reply. So I posted it here:
array = ArrayUtils.removeElement(array, element);
I was wondering how this would be applied in code, as this is just the code:
Would it look something like this:
myArray = ArrayUtils.removeElement(myArray, 2);
or
myArray = ArrayUtils.removeElement(int[], 2);

The first line
myArray = ArrayUtils.removeElement(myArray, 2);
is correct syntax. It would return a new version of your array, with the element at index 2 removed. The other line would result in an error, because you aren't actually passing an array object but rather just a type.
Here's the JavaDoc for ArrayUtils if you would like to learn more about this method or other, related methods: https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html

Related

Can't grasp how to use System.arraycopy(); [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am attempting to copy all of the elements in an array into another one, however I am not grasping the concept of System.arraycopy(). Yes, I have researched into it and can't grasp the concept.
Parameters of arraycopy,
arraycopy(Object source, int sourceIndex, Object destination, int destinationIndex, int length);
An Example, Lets say we have 2 arrays each with 5 elements.
int arrayOne[] = {10,20,30,40,50}; // 5 Elements Each.
int arrayTwo[] = {2,4,6,8,10};
If we want to replace the 3rd element (2nd Index) of arrayTwo[2] with the 5th Element (4th Index) of arrayOne[4] we do the following.
System.out.println("Before (System.array.copy): "+arrayTwo[2]);
System.arraycopy(arrayOne,4,arrayTwo,2,1);
System.out.println("After (System.array.copy) : "+arrayTwo[2]);
The Output is as follows.
Before System.array.copy: 6
After System.array.copy : 50

If A is an array of type int, what command would you use to put the value 50 in the first position of the array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
If A is an array of type int, what command would you use to put the value 50 in the first position of the array
A[0] = 50;
This will store 50 in the first element of A.
A[0] = 50; This assign first element to 50.
There is a lot of resource about java array online, i think all the basic tutorial would cover about this.

How can I find how many different words have on the linked list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have been trying for a while to do a method that return, number of different words occurrences on the list. And it wasn't success.
Any help would be appreciated!
You can add the words in a Set and at the end count the elements present in the Set.
A set is a data structure similar to a List that doesn't allow duplicates.
public int countDistinctWords(List<String> words) {
Set<String> distinctWords = new HashSet<String>(words);
return distinctWords.size();
}
Copy the linked list into a (hash) set, and look at its size:
System.out.println(new HashSet<>(yourLinkedList).size());
If you're using Java 8 you could use the Stream Api
public long countDistinct(List<String> list){
return list.stream().distinct().count();
}

Is there any fast way to remove an element from a string array in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
array = ArrayUtils.removeElement(array, array[i]);
This is working for int array, not for String array...
Is there any fast way to remove an element from a string array in Java?
Seems that you are using org.apache.commons.lang.ArrayUtils class, which is not standard Java class but part of Apache Commons Lang library. If you know an index of element, it would be more efficient to use
array = ArrayUtils.remove(array, i);
As this version will not search for given element, just remove by index. This should work for object arrays (including String array) as well.
Since array can not be resized, it may create some problems (eg.- raise of cost) when you try to remove some element from an array.
In this case ArrayList would be a better alternative. You can add/remove element from ArryList more continently -
List<String> strgs = new ArrayList<String>();
strgs.add("first");
strgs.add("second");
strgs.add("third");
strgs.remove("second");

trying to obtain a first name in the path after first back slash [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to display the first folder name from the path.
/mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java
/mnt/sdcard/Videos/lk.jpeg--> **mnt**
You can split on / and use [1] element from result array.
You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).
String desired = "/mnt/sdcard/foo".split("/")[1];
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);
Try this out. This is a poor question. But maybe the asker can be a newbie.

Categories