I am trying to replace a value in a List<List<String>> in Java and I am facing this error. I am using List.set() to do this. Basically in my program I am trying to replace the first element of every list in dataList to the variable x.
Below is my code:
for(int i = 0; i <= dataList.size()-1; i++) {
String x = String.join("-","Water",dataList.get(i).get(8).replaceAll("\\s","").split("&")[0],dataList.get(i).get(0));
dataList.set(dataList.get(i).get(0),x);
}
} //end main
But I encountered this error "The method set(int, List<String>) in the type List<List<String>> is not applicable for the arguments (String, String)"
And then I tried converting the sentence to:
dataList.set(Integer.parseInt(dataList.get(i).get(0)),x);
Anyone has any idea how to resolve this ?
you must call set on the inner lists, not the list holding other lists.
It is more like this
for(int i = 0; i <= dataList.size()-1; i++) {
String x = String.join("-","Water",dataList.get(i).get(8).replaceAll("\\s","").split("&")[0],dataList.get(i).get(0));
dataList.get(i).set(0, x);
}
You get list number i and set the first value (0) to x.
Cheers
Related
I'm trying to figure out how to write logic to convert an array of string into an ArrayList weekday object
String[] weekdays = request.getParameterValues("weekday");
I've made my ArrayList weekday2 the length of my String[] weekdays.
ArrayList<WeekDay> weekdays2 = new ArrayList<WeekDay>(weekdays.length);
I know there is a way to utilize enums, and that's what I'm trying to do.
for (int i = 0; i < weekdays.length; i++) {
weekdays2.add(new WeekDay().valueOf(weekdays2, weekdays));
}
I know I'm definitely missing something.
error:
The method valueOf(Class<T>, String) in the type Enum<WeekDay> is not applicable for the arguments (ArrayList<WeekDay>, String[])
Code to get list of WeekDay may be such:
for (int i = 0; i < weekdays.length; i++) {
weekdays2.add(WeekDay.valueOf(weekdays[i]));
}
I have below list defined,
List<BigDecimal> empList = new ArrayList<BigDecimal>();
And as per below code if condition throws error The type of the expression must be an array type but it resolved to List
for(int i1=0;i1<12;i1++) {
if(empList[i1]==null){
empList[i1]= new BigDecimal("0.00");
}
}
The same code works well in Groovy but does not work in Java.
Looks like a syntax error . In java list are accessed as
empList.get(i1)
and
empList.set(i1,new BigDecimal("0.00"))
or you are free to use array instead of List but in this case you will have to define the array length which stays fixed.
BigDecimal[] empList = new BigDecimal[10];
It has to be :
for (int i1 = 0; i1 < 12; i1++) {
if (empList.get(i1) == null) {
empList.set(i1, new BigDecimal("0.00"));
}
}
A list collection needs has its own methods. Use those.
I'm trying to loop through an ArrayList an get the current value of the loop instance.
public ArrayList<Double> getCurrent(double x) {
ArrayList<Double> list = new ArrayList<>();
list.add(x);
ArrayList<Double> list1 = new ArrayList<>();
double sum = 0;
for (int i = 0; i < list.size(); i++) {
sum += list.get(i);
list1.add(sum);
}
return list1;
}
Here are my JSP file. Note the loop goes through my ArrayList history, and the values I want to parse into the new ArrayList from the method getCurrent(double x) takes ret.
<% for (history his : history) {
double ret = his.getRet();
double dep = his.getDeposit();
String res = his.getRes();
if (res.equals("Loss")) {
ret = -dep;
}
%>
<h1><%=his.getCurrent(ret) %></h1>
<%} %>
This gives my an ArrayList which consist of the correct values, but NOT added with the previous values. The ArrayList now shows: [30.0, -5.0, 20.0] which is correct values. However, I want it to display: [30, 25, 45] so the values are added together with all previous values.
I believe what you want to do is to pass the double values to this function and then the function will return the list with the calculated values.
According to your code whenever the function is invoked a new Arraylist is declared every time. Thus there are no previous values, there. If you want the output to be like what you have mentioned then declare the list outside the function. Once it is outside there is no need to declared it again and again. It will retain all the previous values and when you will do your calculation it will give you your expected output.
I believe you want to get some but your declared variable is not final. Every function call will reset its value and you will not be able to get the required results. Furthermore, the statement:
sum += list.get(i);
is not as much appropriate to all compilers and IDE's and this can cause runtime error and may contain garbage if not properly handled. I prefer using this modified code.
Final double sum = 0;
for (int i = 0; i < list.size(); i++) {
sum = sum + list.get(i);
list1.add(sum);
}
I am getting some value from a HTML Table. I am able to print the values but not able to figure out a way by which i can sort them alphabetically.
Code which i used-
List<WebElement> lst = driver.findElements(By.xpath(".//*[#class='user-questions lines']/tbody/tr/td[2]"));
for(WebElement s : lst) {
System.out.println(s.getText()); //This line prints the value
String[] ss = new String[lst.size()];
int i=0;
if(s.getText() != null) {
ss[i]=s.getText();
System.out.println(ss[1]); // But here i am getting null
}
i++;
}
First i tried to sort it using Collections.sort(lst) but got an error message:
Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (List).
Then i tried to put the string value inside a String array and later sort it but here i am getting null value when i tried to put the value into the String array
Today i was stuck in the same thing and found this question asked a way back at the start of this year. When i tried the solutions given i got a NullPointerException.
I wrote a small code and now i am able to short the webelements accoriding to the Natural alphabetical order. Here is the code-
//Here storing the WebElements in the List
List<WebElement> lst = driver.findElements(By.xpath("WebElement XPath"));
//Here creating a String array
String[] str = new String[lst.size()];
for (int i = 0 ; i<str.length ; i++){
str[i] = driver.findElements(By.xpath("WebElement XPath")).get(i).getText();
}//for
Arrays.sort(str);
for(String strr: str){
System.out.println(strr);
}
driver_1.close();
driver_1.quit();
}//try
catch(Exception e){
e.printStackTrace();
driver.close();
driver.quit();
}//catch
First i stored the webelements in the List. Then i created a String array for sorting.In the first for loop i stored the string which i got from the website. After that i sorted them using Arrays.sort method
In the advance for loop i am printing the output.
As i can see the question is very old and if during this time you got much better answer then please share it.
You getting null at System.out.println(ss[1]);
because:
ss[i]=s.getText(); always put in ss[0], because: before if int i=0;.
So ss[1] always empty.
To solve it put int i=0; before for(WebElement s : lst)
Declare i outside foreach loop. and sysout ss[i]. On your first loop execution obviously ss[1] will be null.
Another way around is:
List<WebElement> lst = driver.findElements(By.xpath(".//*[#class='user-questions lines']/tbody/tr/td[2]"));
String[] str = new String[lst.size()];
int i = 0;
for (WebElement ele : lst) {
if(ele.getText()!=null)
str[i] = ele.getText();
i++;
}
Arrays.sort(str); // to sort the array of strings
How to Print the list elements separately using Java
.i.e., I have a list of elements with different calculatedvalue and different updatevalues. I want to display calvalue1 and updateval1 separately into two different variables say calvalue1 into double calValue and updateval1 into double updateValue.And the loop should iterate until list becomes empty.Can anyone provide me the logic please help me.
list has at 1st line 0.98 and 23/12/2005 (list.get(0))
at 2nd line 2.3 and 08/09/2013 (list.get(1))
and so on
//here WebAvail is my class
List<WebAvail> list= new List<WebAvail>();
String query="select calvalue,updateval from sample where url='www.abc.com';
list=session.createQuery(query).list;
for(int i=0;i<list.size();i++)
{
for(int j=0;j<list.get(i);j++)
{
list.get(j);
}
}
i.e., list.get(0) gives me 1st line of the list.In that line I am having calvalue1 and updateval1 say 0.98 and 23/12/2005.I need those values seperately i.e., 0.98 into one variable and 23/12/2005 into another variable(that is I want to access seperately).list.get(0).get(0) is not valid.
How can I get that one.And also If I do like in the above code snippet It is giving me "ClassCastException:unable to cast java.lang.object to java.util.list".Here I am not having problem with query and execution of query.I checked the list by forwarding it to jsp.And jsp displaying list values.But I want list elements in WebAvail separately as I mentioned above.
for(int i=0;i<list.size();i++)
{
String a[] = list.get(j).toString().split(",");
Double a1 = Double.parseDouble(a[0].toString());
String a2 = a[1].toString();
}
Is your list.get(0) also returning the list? In this case, you can do the following:
double calValue;
Date updateval1;
List listEle = null;
for(int i=0;i<list.size();i++){
listEle = (List) list.get(i);
if(null != listEle){
if(listEle .get(0) instanceof Double)
calValue = (Double) listEle .get(0);
if(listEle .get(1) instanceof Date)
updateval1 = (Date) listEle .get(1);
}
}