To remove null values while traversing arraylist - java

I am trying with this code to replace null values in arraylist. I am getting null values in a tag in my xml file. Values in that tag are coming from arraylist. I want to remove null from tag and put nothing in place of it. My code is something like this:
for(String s:a.getList){
here I setting values in tag by create tag and than appending child nodess using DOM parser.
}
where a=object that contains list
output is like this:
<value>1</value>
<value>2</value>
<value>null</value>
<value>3</value>
<value>4</value>
<value>null</null>
.
.
.and so on
Expected output:
<value>1</value>
<value>2</value>
<value/>
<value>3</value>
<value>4</value>
<value/>
null should be removed and tag should look something like this
code I am trying is:
for(String s:a.list){
if(s.equals("null")){
s.replace("null","");
my code;
}
Always getting null pointer exception and don't know if this runs what will be output.
Please help..

You are not updating the list, you are creating a new String instance since String are immutable. Just set the value you want if the current value is "null"
for(int i = 0; i < list.size(); ++i){
if("null".equals(list.get(i)){
list.set(i, "");
}
}
The condition won't fail for null value, but if you want to replace those, you need to add the condition because for now, it will only update "null".

The best way to approach using array list is iterate from last to first if you want to remove concurrently.
for (int i = list.size()-1; i >= 0; i--) {
if ("null".equalsIgnoreCase(list.get(i))) {
list.remove(i);
}
}
"null".equalsIgnoreCase(list.get(i)) will avoid null pointer exception
For removing and printing value
for (String str : abc) {
if ("null".equalsIgnoreCase(str)) {
System.out.println("<value/>");
} else {
System.out.println("<value>"+str+"</value>");
}
}

To make it clearer
public static void main(String... args) {
ArrayList<String> a = new ArrayList<String>();
a.add("one");
a.add(null);
a.add("two");
a.removeAll(Collections.singleton(null));
for(String value : a) {
System.out.println(value);
}
}
Output
one
two

Related

Check if List <String> contains substring

I'm developing an Android app where I get a List of names (Strings) from a SQLite database. Then, I show them on a List of Cardviews.
I'm also given a substring, so I'd like to check if any of the elements of the original List contains that substring on it.
I'm having problems using LIKE in the SQLite query, so I've just fixed it with a try{ } catch (Exception e) { }, but I don't feel so comfortable with it.
Anyway, since I originally have all of the names stored in the List of Strings, I don't really need to fecth the new ones again from the database, I can just search them on the original List of Strings. But I don't know how.
For example, let's say the List of String has this 5 elements:
Hello
Wall
Helicopter
Chair
Hell
And I'm given the substring Hel. I should get the Hello, Helicopter and Hell strings, since all of them contain the substring Hel.
Any ideas on how to achieve this goal?
It's not that I want to fix it this way. I sincerely would like to know what's the most efficient, if retrieveing the data again from the database, or search it from the List of Strings.
EDIT: I originally said that I was using the CONTAINS query, but I missed the word. I just meant the LIKE query. I'm actually using it, and it works fine but I don't really know what's the most efficient in my case.
Thanks in advance.
Here is my suggestion
public boolean isSubstring(String str, String key) {
if(key.length() > str.length()) return false;
for(int i = 0; i <= str.length() - key.length(); i++) {
if( key.equals(str.substring(i, i + key.length())) ) return true;
}
return false;
}
public List<String> findStings(List<String> list, String key) {
List<String> newList = new ArrayList<>();
for(int i = 0; i < list.size(); i++) {
if(isSubstring(list.get(i), key)) {
newList.add(list.get(i));
}
}
return newList;
}
If you are working with Java 8 you can replace the for-loop of the second method as:
for(String str: list) {
if(isSubstring(str, key)) newList.add(str);
}
or simply as:
list.stream().filter((str) -> (isSubstring(str, key))).forEach((str) -> { newList.add(str); });
If you're using Java 8:
List<String> filteredList = myList.stream().filter(s -> s.contains("Hel")).collect(Collectors.toList());
If you're using anything earlier, use a for each loop:
List<String> filteredList = new ArrayList<String>();
for(String s : myList)
if(s.contains("Hel")
filteredList.add(s);

why won't my for loop work to print out value in the text file?

I know it is something simple but for some reason i cannot get this loop to work. Help is much appreciated
public Instructor()
{
// initialise instance variables
writeMap = new HashMap<String,String>();
notRecognised = new ArrayList<String>();
help = new FileHelper();
notRecognised = help.readAList("missed.txt");
for(String i : notRecognised.length) {
System.out.println(i);
}
}
Your enhanced for loop isn't correct. Use the list, not it's length
for (String i : notRecognised) {
System.out.println(i);
}
You'll want to iterate over a collection, not over a length:
for ( String i : notRecognized )
See the bottom example in the Java for tutorial.
Probably missed.txt is empty and hence the help.readAList() function returns empty String.
notRecognised = help.readAList("missed.txt");

How can I add a string one at a time to a HashMap<Integer, List<String>>?

This function loops through a dictionary (allWords) and uses the
getKey function to generate a key. wordListMap is a HashMap> so I need to loop through and put the key and and a List. If there is not a list I put one if there is I just need to append the next dictionary word. This is where I need help. I just can't figure out the syntax to simply append the next word to the list that is already there. Any Help would be appreciated.
public static void constructWordListMap() {
wordListMap = new HashMap<>();
for (String w : allWords) {
int key = getKey(w);
if (isValidWord(w) && !wordListMap.containsKey(key)) {
List list = new ArrayList();
list.add(w);
wordListMap.put(key, list);
} else if (isValidWord(w) && wordListMap.containsKey(key)) {
wordListMap.put(key, wordListMap.get(key).add(w));
}
}
}
map.get(key).add(value)
Simple as that.
So I've gathered that you want to, given HashMap<Integer, List<String>>, you'd like to:
create a List object
add String objects to said List
add that List object as a value to be paired with a previously generated key (type Integer)
To do so, you'd want to first generate the key
Integer myKey = getKey(w);
Then, you'd enter a loop and add to a List object
List<String> myList = new List<String>;
for(int i = 0; i < intendedListLength; i++) {
String myEntry = //wherever you get your string from
myList.add(myEntry);
}
Lastly, you'd add the List to the HashMap
myHash.put(myKey, myList);
Leave any questions in the comments.
else if (isValidWord(w) && wordListMap.containsKey(key)) {
wordListMap.put(key, wordListMap.get(key).add(w));
}
If you want to add a new value to your list, you need to retrieve that list first. In the code above, you are putting the return value of add into the table (which is a boolean), and that is not what you want.
Instead, you will want to do as Paul said:
else if (isValidWord(w) && wordListMap.containsKey(key)) {
wordListMap.get(key).add(w);
}
The reason this works is because you already added an ArrayList to the table earlier. Here, you are getting that ArrayList, and adding a new value to it.

Writing a method with ArrayList of strings as parameters

I am trying to write a method that takes an ArrayList of Strings as a parameter and that places a string of four asterisks in front of every string of length 4.
However, in my code, I am getting an error in the way I constructed my method.
Here is my mark length class
import java.util.ArrayList;
public class Marklength {
void marklength4(ArrayList <String> themarklength){
for(String n : themarklength){
if(n.length() ==4){
themarklength.add("****");
}
}
System.out.println(themarklength);
}
}
And the following is my main class:
import java.util.ArrayList;
public class MarklengthTestDrive {
public static void main(String[] args){
ArrayList <String> words = new ArrayList<String>();
words.add("Kane");
words.add("Cane");
words.add("Fame");
words.add("Dame");
words.add("Lame");
words.add("Same");
Marklength ish = new Marklength();
ish.marklength4(words);
}
}
Essentially in this case, it should run so it adds an arraylist with a string of "****" placed before every previous element of the array list because the lengths of the strings are all 4.
BTW
This consists of adding another element
I am not sure where I went wrong. Possibly in my for loop?
I got the following error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at Marklength.marklength4(Marklength.java:7)
at MarklengthTestDrive.main(MarklengthTestDrive.java:18)
Thank you very much. Help is appreciated.
Let's think about this piece of code, and pretend like you don't get that exception:
import java.util.ArrayList;
public class Marklength {
void marklength4(ArrayList <String> themarklength){
for(String n : themarklength){
if(n.length() ==4){
themarklength.add("****");
}
}
System.out.println(themarklength);
}
}
Ok, so what happens if your list just contains item.
You hit the line if(n.length() ==4){, which is true because you are looking at item, so you go execute its block.
Next you hit the line themarklength.add("****");. Your list now has the element **** at the end of it.
The loop continues, and you get the next item in the list, which happens to be the one you just added, ****.
The next line you hit is if(n.length() ==4){. This is true, so you execute its block.
You go to the line themarklength.add("****");, and add **** to the end of the list.
Do we see a bad pattern here? Yes, yes we do.
The Java runtime environment also knows that this is bad, which is why it prevents something called Concurrent Modification. In your case, this means you cannot modify a list while you are iterating over it, which is what that for loop does.
My best guess as to what you are trying to do is something like this:
import java.util.ArrayList;
public class Marklength {
ArrayList<String> marklength4(ArrayList <String> themarklength){
ArrayList<String> markedStrings = new ArrayList<String>(themarklength.size());
for(String n : themarklength){
if(n.length() ==4){
markedStrings.add("****");
}
markedStrings.add(n);
}
System.out.println(themarklength);
return markedStrings;
}
}
And then:
import java.util.ArrayList;
public class MarklengthTestDrive {
public static void main(String[] args){
ArrayList <String> words = new ArrayList<String>();
words.add("Kane");
words.add("Cane");
words.add("Fame");
words.add("Dame");
words.add("Lame");
words.add("Same");
Marklength ish = new Marklength();
words = ish.marklength4(words);
}
}
This...
if(n.length() ==4){
themarklength.add("****");
}
Is simply trying to add "****" to the end of the list. This fails because the Iterator used by the for-each loop won't allow changes to occur to the underlying List while it's been iterated.
You could create a copy of the List first...
List<String> values = new ArrayList<String>(themarklength);
Or convert it to an array of String
String[] values = themarklength.toArray(new String[themarklength.size()]);
And uses these as you iteration points...
for (String value : values) {
Next, you need to be able to insert a new element into the ArrayList at a specific point. To do this, you will need to know the original index of the value you are working with...
if (value.length() == 4) {
int index = themarklength.indexOf(value);
And then add a new value at the required location...
themarklength.add(index, "****");
This will add the "****" at the index point, pushing all the other entries down
Updated
As has, correctly, been pointed out to me, the use of themarklength.indexOf(value) won't take into account the use case where the themarklength list contains two elements of the same value, which would return the wrong index.
I also wasn't focusing on performance as a major requirement for the providing a possible solution.
Updated...
As pointed out by JohnGarnder and AnthonyAccioly, you could use for-loop instead of a for-each which would allow you to dispense with the themarklength.indexOf(value)
This will remove the risk of duplicate values messing up the index location and improve the overall performance, as you don't need to create a second iterator...
// This assumes you're using the ArrayList as the copy...
for (int index = 0; index < themarklength.size(); index++) {
String value = themarklength.get(index);
if (value.length() == 4) {
themarklength.add(index, "****");
index++;
But which you use is up to you...
The problem is that in your method, you didn't modify each string in the arraylist, but only adds 4 stars to the list. So the correct way to do this is, you need to modify each element of the arraylist and replace the old string with the new one:
void marklength4(ArrayList<String> themarklength){
int index = 0;
for(String n : themarklength){
if(n.length() ==4){
n = "****" + n;
}
themarklength.set(index++, n);
}
System.out.println(themarklength);
}
If this is not what you want but you want to add a new string "**" before each element in the arraylist, then you can use listIterator method in the ArrayList to add new additional element before EACH string if the length is 4.
ListIterator<String> it = themarklength.listIterator();
while(it.hasNext()) {
String name = it.next();
if(name.length() == 4) {
it.previous();
it.add("****");
it.next();
}
}
The difference is: ListIterator allows you to modify the list when iterating through it and also allows you to go backward in the list.
I would use a ListIterator instead of a for each, listiterator.add likely do exactly what you want.
public void marklength4(List<String> themarklength){
final ListIterator<String> lit =
themarklength.listIterator(themarklength.size());
boolean shouldInsert = false;
while(lit.hasPrevious()) {
if (shouldInsert) {
lit.add("****");
lit.previous();
shouldInsert = false;
}
final String n = lit.previous();
shouldInsert = (n.length() == 4);
}
if (shouldInsert) {
lit.add("****");
}
}
Working example
Oh I remember this lovely error from the good old days. The problem is that your ArrayList isn't completely populated by the time the array element is to be accessed. Think of it, you create the object and then immediately start looping it. The object hence, has to populate itself with the values as the loop is going to be running.
The simple way to solve this is to pre-populate your ArrayList.
public class MarklengthTestDrive {
public static void main(String[] args){
ArrayList <String> words = new ArrayList<String>() {{
words.add("Kane");
words.add("Cane");
words.add("Fame");
words.add("Dame");
words.add("Lame");
words.add("Same");
}};
}
}
Do tell me if that fixes it. You can also use a static initializer.
make temporary arraylist, modify this list and copy its content at the end to the original list
import java.util.ArrayList;
public class MarkLength {
void marklength4(ArrayList <String> themarklength){
ArrayList<String> temp = new ArrayList<String>();
for(String n : themarklength){
if(n.length() ==4){
temp.add(n);
temp.add("****");
}
}
themarklength.clear();
themarklength.addAll(temp);
System.out.println(themarklength);
}
}

how can I test in Java if an Object exists at specific index?

I use Arraylist and inserting Objects to specific index.(for example is element at index of 0 and 2 but it is not at at index of 1)And I want to know if I should use Arraylist.add(id,obj) or Arraylist.set(id,obj) . I use the following test if(Arraylist.get(t.imgIdx) == null) , but it throws me Exception out of bounds all the time. How can/should I test it?
public static int GiveBackAverageID(Vector<DMatch> lista){
ArrayList<CMatch> workingList = new ArrayList<CMatch>();
for (DMatch t : lista){
if(workingList.get(t.imgIdx) == null){
workingList.add(t.imgIdx, new CMatch(t.imgIdx,t.distance,1));
}else{
CMatch pom = workingList.get(t.imgIdx);
pom.setSummaDist(pom.getSummaDist()+t.distance);
pom.setCount(pom.getCount()+1);
workingList.set(t.imgIdx, pom);
}
}
...
thanks
Csabi
If you want to test if an object is at a position, use IndexOf(). This method returns -1 if the object is not in the list.
UPDATE
On your new piece of code:
public static int GiveBackAverageID(Vector<DMatch> lista){
ArrayList<CMatch> workingList = new ArrayList<CMatch>();
for (DMatch t : lista){
if(t.imgIdx >= workingList.size() || t.imgIdx < 0)
{
// do something with wrong indices.
}
else
{
if(workingList.get(t.imgIdx) == null){
workingList.add(t.imgIdx, new CMatch(t.imgIdx,t.distance,1));
}else{
CMatch pom = workingList.get(t.imgIdx);
pom.setSummaDist(pom.getSummaDist()+t.distance);
pom.setCount(pom.getCount()+1);
workingList.set(t.imgIdx, pom);
}
}
}
}
Or what you could also do, is generate more capacity in your workingList:
public static int GiveBackAverageID(Vector<DMatch> lista){
// Creating more capacity in the constructor!
ArrayList<CMatch> workingList = new ArrayList<CMatch>(lista.size());
for (DMatch t : lista){
if(workingList.get(t.imgIdx) == null){
workingList.add(t.imgIdx, new CMatch(t.imgIdx,t.distance,1));
}else{
CMatch pom = workingList.get(t.imgIdx);
pom.setSummaDist(pom.getSummaDist()+t.distance);
pom.setCount(pom.getCount()+1);
workingList.set(t.imgIdx, pom);
}
}
}
As a better alternative, I would use a HashTable<int,DMatch> instead.
use set to replace at a specific index, and add to add object at the end, and add with index value to insert object at position and move other elements to right (adding one to their index).
Out of bounds exception means your index value is, probably, to large; or the arraylist has not been populated as you expect. Post the full exception/code for a complete answer.
YOu need to check the size of the list before you try to insert. If the index is less than the list size, then you should be ok to check to see if it is null, otherwise, you always want to add a new element
I think the problem with your code is that t.imgIdx value is sometimes greater than the array size.
But when you access the element (which is supposed to be null) like the code you did
if(workingList.get(t.imgIdx) == null), your if condition will return boolean value provided that the parameter passed to get() is less than the size of the array.
You can try the below example and pass different parameter values to get() method.
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(0, "5");
al.add(1, "10");
al.add(2, "15");
al.add(3, "20");
al.set(1, null);//setting the element at index 1 to NULL.
//al.remove(1);//removes the element in list so that the next element's index decreases by 1.
if(al.get(1) == null){
System.out.println("Nothing here..");//this line executes
}
else
System.out.println(al.get(1));
}
Catch the Exception and handle it properly, it says that there is no element with this index.

Categories

Resources