cannot convert from ArrayList<Integer> to ArrayList<String> [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 have a ArrayList with integer value and I want to read this value from another ArrayList consist string value. How can I do that ?
public static ArrayList<Integer> scorerValue = new ArrayList<Integer>();
and I want read here using
ArrayList<String> score_Number ;

Okay. So let's first declare our List<String>..
List<String> score_Number = new ArrayList<String>();
// Remember you program to an interface. Not a concrete implementation.
Then to convert you..
// Do some magic.
That's right Magic. Magic involving the toString() method. You will need to call toString() in a for each loop. Finally, you will need to use the List.add() method, to add the newly created String values to your list score_Number.
NOTE: Follow the naming conventions. score_Number should be scoreNumber.

Try,
ArrayList<Integer> integerList = new ArrayList<Integer>();
// add Integer Elements
ArrayList<String> stringList = new ArrayList<String>();
for(Integer i : integerList){
stringList.add(i+"");
}

try this:
ArrayList<Integer> scorerValue = new ArrayList<Integer>();
scorerValue.add(1);
ArrayList<String> score_Number = new ArrayList<String>();
for (Integer i : scorerValue)
score_Number.add(i.toString());
System.out.println(score_Number);

Related

How to convert an ArrayList<Object> to an ArrayList<Double> in Java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to convert a List of Objects which contains only numbers such as Doubles and Integers to a new List which is a List of Double. How can I do that?
List<Object> o = new ArrayList<>();
example: o contains o = {1,1.0,3,5.6,7.3};
List<Double> list = new ArrayList<>();
I tried using the stream() method but no luck there.
Maybe this helps you:
list = o.stream().filter(t -> t instanceof Double).map(t -> (Double) t).collect(Collectors.toList());
You don't need to use streams - just cast it to a raw List like so:
List<Object> o = new ArrayList<>();
List<Double> list = (List) o;

Java Remove ArrayList by Value [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 5 years ago.
Improve this question
I have an ArrayList like this:
[{1=R111, 2=Red, 3=50000}, {1=R123, 2=Blue , 3=50000}]
and i want to remove the array by value (R111 or R123).
how to remove the array using array.remove method for array like that?
I've try this link
but it's doesn't work for my problem.
Assuming your ArrayList is this:
List<String[]> arrayList = new ArrayList<>();
arrayList.add(new String[]{"R111","Red","50000"});
arrayList.add(new String[]{"R123","Blue","50000"});
you can do something like:
for (Iterator<String[]> iterator = arrayList.iterator();iterator.hasNext();) {
String[] stringArray = iterator.next();
if("R111".equals(stringArray[0])) {
iterator.remove();
}
}
You can safely remove an element using iterator.remove() while iterating the ArrayList. Also see The collection Interface.
An alternative shorter approach using Streams would be:
Optional<String[]> array = arrayList.stream().filter(a -> "R111".equals(a[0])).findFirst();
array.ifPresent(strings -> arrayList.remove(strings));
Thanks pieter, I used Iterator like this:
for (Iterator<HashMap<String, String>> iterator = RegulerMenu.iterator(); iterator.hasNext();) {
HashMap<String, String> stringArray = iterator.next();
if("R111".equals(stringArray.get("1"))) {
iterator.remove();
}
}
It's work now, Thankyou verymuch.

How to create and fill a list of arrays 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 5 years ago.
Improve this question
I have the field "listofRows":
private List<String[]> listOfRows;
This list is supposed to hold several string arrays.
I then read in a line from a csv file, split the line into separate parts (using the commas as separators) and save the resulting strings in a string array:
String[] stringArray = line.split(",");
According to the debugger, the array stringArray now holds the two words from the CSV-file ("test" and "one").
The problem is with the next line of code:
listOfRows.add(stringArray);
This line does not work.
I can't add the array to the list.
How can I add an array to an arraylist?
NB: I know how I could add the elements of an array to a list separately. This question has already been answered on StackOverflow.
However, I want to add the array as a whole (!).
That is, every element of the list is supposed to be an array.
You need to instantiate your list:
private List<String[]> listOfRows = new ArrayList<String[]>();
In constructor initialize your list and return unmodified list to outside
public CSVReader(){
this.br = null;
this.line = "";
this.splitSign = ",";
listOfRows =new ArrayList<>();
}
Initialize the list or else you will get null pointer exception
public CSVReader(){
this.br = null;
this.line = "";
this.splitSign = ",";
listOfRows =new ArrayList<>(); //Initialization
}
You need to initialize your List and then add the string array to it.
private List<String[]> listOfRows = new List<String[]>();
ListOfRows.add(stringArray);
This would work without throwing any error such as Null pointer: not initialized.

creating arrayList of arraylists [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 was trying to create an ArrayList of ArrayLits. Then, whenever I added an ArrayList to my initial ArrayList, I get My 2nd ArrayList in the initial ArrayList copied to the first ArrayList in the initial ArrayList. Here is my Code. Thank you for any help.
ArrayList<ArrayList> msebi = new ArrayList<ArrayList>();
ArrayList<Pair> temp = new ArrayList<Pair>();
Stack<ArrayList> arrayListStack = new Stack<ArrayList>();
Pair pair = null;
for(int i=1;i<finalList.size()-1;i++){
pair = finalList.get(i);
if(finalList.get(i+1).getDepth() > finalList.get(i).getDepth()){
temp.add(pair);
if(i == finalList.size()-2){
temp.add(finalList.get(i+1));
msebi.add(temp);
}
else {
temp.add(pair);
msebi.add(temp);
temp.clear();
}
}
Kindly Note that I have created another Class that is Named Pair. This Pair consist of a String and depth, and thus we have getDepth(); Also, I have in the main class ArrayList finalList = new ArrayList();
Thus, Whenever I am trying to copy the content of the ArrayLists within the msebi ArrayList, I got the values of the second arraylist the same as the first one.
When you add your ArrayList called temp to your outer ArrayList called msebi, msebi contains a reference to temp, not a copy. Then you immediately clear() temp and build it up again. You have multiple references to the same temp list in msebi.
Create a new ArrayList instead of clearing the old one. This way, when a list is added, it's left alone, because you will be operating on another list object.

Data defined String to Array [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 9 years ago.
Improve this question
I'm working on a game and I'm implementing objects where you can define in "TiledMap editor" what items a certain object holds.
So I've got to an idea where I can enter the Item ID's right there like {22:4, id:amount}. When I parse the map, I retrieve that array as a string, is there a way to convert it to an array?
Thanks in advance!
Firstly, you probably want a Map, not an array or a List.
Map<String,String> processParams(String list) {
Map<String,String> = new HashMap<String,String>();
int openBracket = list.indexOf("{");
int closeBracket = list.lastIndexOf("}");
String params = list.substring(openBracket+1,closeBracket);
String paramList = params.split(",");
for(String param: paramList) {
String pData = param.trim().split(":");
map.put(param[0].trim(),param[1].trim());
}
return map;
}
processParams("{22:4, id:amount}");
Of course, it's actually a JSON-like structure so there's probably pre-existing parsers.

Categories

Resources