I have a list
List<List> rows = (List<List>) responseMap.get("data");
[[FRPP, PE103, , USD], [FRPP, PE313AHMR, , USD]
And I want to set the data to the fields of a bean (which represents all the fields for each line)
ArrayList data = new ArrayList();
for (int i = 0; i < 10; i++) {
Bean line = new Bean();
line.setField1("element1");
line.setField2("element2");
line.setField3("element3");
line.setField4("element4");
data.add(line);
}
How can I do that?
Using JDK 1.6, Windows
You are getting list in the format of [[FRPP, PE103, , USD], [FRPP, PE313AHMR, , USD].
Use ArrayList.get(int) to get value of each index value.
for (int i = 0; i < rows.size(); i++) {
Bean line = new Bean();
ArrayList al=(ArrayList)rows.get(i);//now [FRPP, PE103, , USD]
line.setField1((String)al.get(0));//FRPP
line.setField2((String)al.get(1));//PE103
line.setField3((String)al.get(2));//
line.setField4((String)al.get(3));//USD
data.add(line);
}
Related
Is it possible to convert this type List<Jadval> into String[] wordList?
I read the words from database with like this :
public static List<Jadval> jadvalList = new ArrayList<Jadval>();
JadvalDB jadvalDB = new JadvalDB(GameActivity.this);
jadvalList = jadvalDB.getWords(myPos + 1);
and now i want to put jadvalList values into String[] wordList.
i use this code to set the values :
for (int i = 0; i < jadvalList.size(); i++) {
wordList[i] = (jadvalList.get(i).toString());
}
but I get the error that wordList is empty .
any idea?
You can use streams and complete it in one line like this:
String[] wordList = jadvalList.stream().map(a->a.toString()).toArray(String[]::new);
I want to get location name from mysql database. I want to retrieve all location name and collect in ArrayList and this List requested by setAttribute to jsp.
ArrayList<Bean> SupplyLocation = new ArrayList<Bean>();
try {
//...
while(rs.next()) {
Bean Location = new Bean();
String supply[] = (rs.getString("location_name")).split(",");
for(int i=0; i<supply.length; i++) {
Location.setLocation(supply);
SupplyLocation.add(Location);
}
}
}
you created only one object Location, so it will be modified later in for loop, so you will end up with one object with supply from supply[lastIndex] in that object, and all references in ArrayList will point to it.
Fixed:
while(rs.next()) {
String supply[] = (rs.getString("location_name")).split(",");
for(int i=0; i<supply.length; i++) {
Bean Location = new Bean();
Location.setLocation(supply[i]);
SupplyLocation.add(Location);
}
}
In this way, you create new object Bean for each string in supply array, then you set string supply[i] to it, and you place a reference to it in SupplyLocation.
Considering that your string is comma seperated, then do this,
String supply[] = (rs.getString("location_name")).split(",");
for(int i=0; i<supply.length; i++) {
Location.setLocation(supply);
SupplyLocation.add(Location.toString());
}
I already generated an ont.owl file using Jena. Then first I need to take all the classes to the array list which are contain the ontology. secondly I will give another classes(terms) using my code and check whether these classes contain generated ontology or not. following is the code up to now.
m.read("http://localhost/myontofile/ont.owl");
ExtendedIterator<OntClass> classes = m.listClasses();
while (classes.hasNext()) {
OntClass takeclasses = (OntClass) classes.next();
String ontcls = takeclasses.getLocalName().toString();
ArrayList<String> listiter = new ArrayList<String>();
listiter.add(ontcls);
System.out.println("classes: " + listiter); ----------????
///////////////////////////////////////
ArrayList<String> tempTerms = new ArrayList<String>();
for(int i=0; i < terms.size(); i++) {
String aTerm = terms.get(i) ;
tempTerms.add(aTerm);
}
terms.add("Information");
terms.add("Video Information");
terms.add("Video Price Information");
terms.add("Video Maximum Price Information");
terms.add("Action Video Price Information");
for(int i=0; i < terms.size(); i++) {
if (listiter.equals(terms.get(i))==true) {
System.out.println("ok");
}
else {
System.out.println("no");
}
}
}
Result is always come to else ("no") part. "classes" give out put as only one class. What are the changes I need to do?
I am working on a JSF based Web Application where I read contents from a file(dumpfile) and then parse it using a logic and keep adding it to a list using an object and also set a string using the object. But I keep getting this error. I am confused where I am wrong. I am a beginner so can anyone be kind enough to help me?
List<DumpController> FinalDumpNotes;
public List<DumpController> initializeDumpNotes()
throws SocketException, IOException {
PostProcessedDump postProcessedDump = (PostProcessedDump) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("postProcessedDump");
List<DumpController> FinalNotes = new ArrayList<>();
if (postProcessedDump.getDumpNotes() == null) {
dumpNotes = new DumpNotes();
}
DumpListController dlcon = (DumpListController) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("dumpListController");
DumpInfo dumpinfo = dlcon.getSelectedDumpInfo();
String fileName = dumpinfo.getDate() + dumpinfo.getTime() + dumpinfo.getSeqNo() + dumpinfo.getType() + dumpinfo.getTape() + dumpinfo.getDescription() + ".txt";
if (checkFileExistsInWin(fileName)) {
postProcessedDump.setDumpnotescontent(getFileContentsFromWin(fileName));
String consolidateDumpnotes = getFileContentsFromWin(fileName);
String lines[];
String content = "";
lines = consolidateDumpnotes.split("\\r?\\n");
List<String> finallines = new ArrayList<>();
int k = 0;
for (int i = 0; i < lines.length; i++) {
if (!lines[i].equalsIgnoreCase("")) {
finallines.add(lines[i]);
k++;
}
}
for (int j = 0; j < finallines.size(); j++) {
if (finallines.get(j).startsWith("---------------------SAVED BY")) {
PostProcessedDump dump = new PostProcessedDump();
dump.setDumpMessage(content);
content = "";
FinalDumpNotes.add(dump);
} else {
content = content + finallines.get(j);
}
}
}
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("postProcessedDump", postProcessedDump);
return FinalDumpNotes;
}
I get the following error:
If you want to add instances of type PostProcessedDump to your List you should change it's type. Also, don't forget to initialize it. Something like,
List<PostProcessedDump> FinalDumpNotes = new ArrayList<>();
Also, Java naming convention is to start variable names with a lower case letter. FinalDumpNotes looks like a class, I would suggest something like
List<PostProcessedDump> processedList = new ArrayList<>();
Problems with your code:
List<DumpController> FinalDumpNotes;
You declare FinalDumpNotes to be a List of DumpController objects, but you never initialize it. In addition, your IDE is barfing on the following line of code:
FinalDumpNotes.add(dump);
because you are attempting to add a PostProcessedDump object to the List instead of a DumpController object.
For starters, you need to initialize your list like this:
List<DumpController> finalDumpNotes = new ArrayList<DumpController>();
Notice that I have made the variable name beginning with lower case, which is the convention (upper case is normally reserved for classes and interfaces).
I will leave it to you as a homework assignment to sort out the correct usage of this List.
requestBuilder.or("userPhone",myPhone,phoneList);
}
}
QBCustomObjects.getObjects("image", requestBuilder, new QBCallbackImpl() {
"phoneList" is arrayList of Strings.
now on my device this code work well but on samsung devices i have crash :
"java.lang.IllegalArgumentException: Illegal character in query at index 147: https://api.quickblox.com/data/image.."
now i know for sure that the arrayList making the problem, because if i put instead of phoneList just , "00000", "09878889" - it's work fine.
what to do?
thanks..
Edit:
ArrayList<String> al = new ArrayList<String>();
HashSet<String> hs = new HashSet<String>();
hs.addAll(phoneList);
al.clear();
al.addAll(hs);
String[]arrString = new String [al.size()+1];
for (int j = 0; j < al.size(); j++) {
String str = al.get(j).toString();
arrString[j+1]= str;
}
arrString[0]= myPhone;
requestBuilder.or("userPhone",arrString);
this is my solution, but i discovered that if "arrString" is bigger than 600+ it's doesn't work, why is that?
Let me explain how OR operator works:
1) for example, you have name field
To get all records with name Alex OR Garry use next query:
requestBuilder.or("name", "Alex", "Garry");
2) for example, you have name field and age field
To get all records with name Alex OR age 22 use next query:
requestBuilder.or("name", "Alex");
requestBuilder.or("age", "22");
Try somtehing like this