Reputation: 45
how to convert ascii value of 49 to actual char in java
my code is below and i am trying to store elements from list which is not start with prefix a or g
void display(){
String[] inpArray={"apple","orange","grapes"};
LinkedList<String> listOne = new LinkedList<String>(Arrays.asList(inpArray));
LinkedList<String> listTwo = new LinkedList<String>();
listTwo.add("melon");
listTwo.add("apple");
listTwo.add("mango");
String[] result1 = {};
for(String res : listOne){
if(res.startsWith("a")||res.startsWith("g")){
System.out.println("--> "+res);
}else{
System.out.println("** "+res);
//result1 = res;//unable to store in string array or to list
// here i have to add all strings how to do that please help me
}
Upvotes: 1
Views: 707
Reputation: 67
Please find the code for storing the element in String array using index i;
package com.test.stackoverflow;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TestClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
display();
}
public static void display() {
String[] inpArray = { "apple", "orange", "grapes" };
List<String> listOne = new ArrayList<String>(Arrays.asList(inpArray));
List<String> listTwo = new ArrayList<String>();
listTwo.add("melon");
listTwo.add("apple");
listTwo.add("mango");
String[] result1 = new String[listOne.size()];
int i =0;
for (String res : listOne) {
if (res.startsWith("a") || res.startsWith("g")) {
System.out.println("--> " + res);
} else {
System.out.println("** " + res);
result1[i++] = res;
}
}
for(String val : result1){
System.out.println("Value"+val);
}
}
}
Upvotes: 2
Reputation: 28289
If you want to store the elements into an array, you can put them into a List
first, then convert the List
to array by List.toArray
:
LinkedList<String> linkedList = new LinkedList<>();
for (String res : listOne) {
if (res.startsWith("a") || res.startsWith("g")) {
System.out.println("--> " + res);
} else {
System.out.println("** " + res);
linkedList.add(res);
}
}
String[] result1 = new String[linkedList.size()];
linkedList.toArray(result1);
Upvotes: 0