And now a very easy java question....
Object[] objectList={
new Object(name[0], description[0], R.drawable.creep_0),
new Object(name[1], description[1], R.drawable.creep_1),
new Object(name[2], description[2], R.drawable.creep_2),
new Object(name[3], description[3], R.drawable.creep_3),
};
How can i do this dynamically with a for cycle? Thanks!
Creep[] creeps = new Creep[]
{ R.drawable.creep_0, R.drawable.creep_1, R.drawable.creep_2,
R.drawable.creep_3 };
Object[] objectList = new Object[4];
for (int i = 0; i < 4; i++) {
objectList[i] = new Object(name[i], description[i], creeps[i]);
}
ArrayList objectList = new ArrayList();
for (int i = 0; i < 4; i++)
{
Object o = new Object(name[i], description[i], R.drawable.creep_i);
objectList.Add(o);
}
Related
When I try to insert a value relating to its key the value is not inserting inside the map. It seems like the "ids" doesn't have any value in it but it does.
Map<String, Vector<String>> graph = new HashMap<String, Vector<String>>();
ArrayList<String[]> words = new ArrayList<String[]>();
Vector<String> ids = new Vector<String>();
String[] id = null;
Scanner scanner = new Scanner(new FileReader(fInName));
while (scanner.hasNextLine()) {
words.add(scanner.nextLine().split("\\s+"));
}
for(int i = 0; i < words.size(); i++) {
id = words.get(i)[1].split("[,]", 0);
for(int j = 0; j < id.length; j++) {
ids.add(j, id[j]);
}
graph.put(words.get(i)[0], ids);
id = null;
ids.clear();
}
for (Map.Entry<String, Vector<String>> entry : graph.entrySet()) {
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
The put() function works by reference. So
put (key, ids)
stores a reference (memory pointer) to the same ids object that you are running the clear() function on. Trying to 'clean up' your memory is laudable, but in this case you're shooting your own foot. What you want is something like this
Map<String, Vector<String>> graph = new HashMap<String, Vector<String>>();
ArrayList<String[]> words = new ArrayList<String[]>();
Vector<String> ids;
String[] id = null;
Scanner scanner = new Scanner(new FileReader(fInName));
while (scanner.hasNextLine()) {
words.add(scanner.nextLine().split("\\s+"));
}
for(int i = 0; i < words.size(); i++) {
id = words.get(i)[1].split("[,]", 0);
ids = new Vector<String>();
for(int j = 0; j < id.length; j++) {
ids.add(j, id[j]);
}
graph.put(words.get(i)[0], ids);
}
So Im doing JUnit Tests. While Im getting the parameters for my test method in a text document I counter an issue that is after the parameter object is not working well. All my parameter pass to the test method at once but I want them to pass separately.
Image below is what I want(Red line means the second set of parameters). The image is a sample run of my test method.
https://ibb.co/nFLhOc
This is my code:
String fileName = "ChargeData.txt";
try
{
scan = new Scanner(new File(fileName));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
ArrayList<Object[]> Params = new ArrayList<Object[]>();
List listOfLists = new ArrayList();
ArrayList <Integer> quantityList;
ArrayList <Boolean> highQualityList;
ArrayList <Boolean> designEffectList;
ArrayList <Double> expectedResultList;
while(scan.hasNext())
{
quantityList = new ArrayList<Integer>();
highQualityList = new ArrayList<Boolean>();
designEffectList = new ArrayList<Boolean>();
expectedResultList = new ArrayList<Double>();
while(scan.hasNextInt())
{
quantityList.add(new Integer(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
highQualityList.add(new Boolean(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
designEffectList.add(new Boolean(scan.next()));
}
expectedResultList.add(new Double(scan.next()));
int[] quantity = new int[quantityList.size()];
boolean[] highQuality = new boolean[quantityList.size()];
boolean[] designEffect = new boolean[quantityList.size()];
double[] expectedResult = new double[1];
for (int i=0; i < quantity.length; i++)
{
quantity[i] = quantityList.get(i).intValue();
highQuality[i] = highQualityList.get(i).booleanValue();
designEffect[i] = designEffectList.get(i).booleanValue();
}
expectedResult[0] = expectedResultList.get(0).doubleValue();
listOfLists.add(quantity);
listOfLists.add(highQuality);
listOfLists.add(designEffect);
listOfLists.add(expectedResult);
}
Params.add(listOfLists.toArray());
scan.close();
return Params.toArray();
}
Code Logic problem, just move the Params.add(listOfLists.toArray()); inside while loop fixed it.
while(scan.hasNext())
{
quantityList = new ArrayList<Integer>();
highQualityList = new ArrayList<Boolean>();
designEffectList = new ArrayList<Boolean>();
expectedResultList = new ArrayList<Double>();
while(scan.hasNextInt())
{
quantityList.add(new Integer(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
highQualityList.add(new Boolean(scan.next()));
}
for(int i=0;i<quantityList.size();i++)
{
designEffectList.add(new Boolean(scan.next()));
}
expectedResultList.add(new Double(scan.next()));
int[] quantity = new int[quantityList.size()];
boolean[] highQuality = new boolean[quantityList.size()];
boolean[] designEffect = new boolean[quantityList.size()];
double[] expectedResult = new double[1];
for (int i=0; i < quantity.length; i++)
{
quantity[i] = quantityList.get(i).intValue();
highQuality[i] = highQualityList.get(i).booleanValue();
designEffect[i] = designEffectList.get(i).booleanValue();
}
expectedResult[0] = expectedResultList.get(0).doubleValue();
listOfLists.add(quantity);
listOfLists.add(highQuality);
listOfLists.add(designEffect);
listOfLists.add(expectedResult);
Params.add(listOfLists.toArray());
}
I have an array with 2 positions, each one contains a list of "ids_alunos", the first position has ids_alunos: "1,2,3" and the second:"4,5" but what is happening when I add the ids in my array is this, the first position gets "1,2,3" values and the second: "1,2,3,4,5". why is this happening?
public ArrayList<CadastraEscolas> getFilhos(String mToken) throws Exception {
String[] resposta = new WebService().get("filhos", mToken);
if (resposta[0].equals("200")) {
JSONObject mJSONObject = new JSONObject(resposta[1]);
JSONArray dados = mJSONObject.getJSONArray("data");
mArrayList = new ArrayList<CadastraEscolas>();
mGPSList = new ArrayList<GPSEscolas>();
for (int i = 0; i < dados.length(); i++) {
JSONObject item = dados.getJSONObject(i).getJSONObject("escolas");
JSONArray escolas = item.getJSONArray("data");
for (int j = 0; j < escolas.length(); j++) {
JSONObject jItens = escolas.getJSONObject(j);
mCadastraEscolas = new CadastraEscolas();
mGPSEscolas = new GPSEscolas();
mCadastraEscolas.setId_escola(jItens.optInt("id_escola"));
mGPSEscolas.setId_escola(jItens.optInt("id_escola"));
JSONObject alunos = jItens.optJSONObject("alunos");
JSONArray data = alunos.getJSONArray("data");
if (data != null) {
ArrayList<Filhos> arrayalunos = new ArrayList<Filhos>();
for (int a = 0; a < data.length(); a++) {
mFilhos = new Filhos();
JSONObject clientes = data.getJSONObject(a);
mFilhos.setId_aluno(clientes.optInt("id_aluno"));
arrayalunos.add(mFilhos);
idsAlunos += ";" + arrayalunos.get(a).getId_aluno().toString();
mGPSEscolas.setIds_alunos(idsAlunos);
}
mCadastraEscolas.setalunos(arrayalunos);
}
mArrayList.add(mCadastraEscolas);
mGPSList.add(mGPSEscolas);
}
}
return mArrayList;
} else {
throw new Exception("[" + resposta[0] + "] ERRO: " + resposta[1]);
}
}
You probably want to initialize idsAlunos at the same time as mGPSEscolas, so where you do:
mGPSEscolas = new GPSEscolas();
you could also do:
idsAlunos = "";
otherwise idsAlunos gets appended to with every new mGPSEscolas.
I'm having one problem in show my ListView correctly
If I declare
String[] cod = {"cod1","cod2", "cod3"};
String[] desc = {"desc1","desc2", "desc3"};
String[] preco = {"1.00","2.00", "3.00"};
My adapter is:
lista = (ListView)findViewById(R.id.list);
lista.setAdapter(new dataListAdapter(cod, desc, preco));
It works perfectly!
But if I get the data from a database, the ListView shows only one row.
results = DbHelper.getItemsDao().queryRaw(sqlGetItems);
/*GET ALL RESULTS FROM DATABASE*/
List<String[]> array = results.getResults();
int count = array.size();
String[] cod = null, desc = null, preco = new String[count];
for(int i = 0; i < count; i++) {
String[] res = array.get(i);
cod = new String[]{res[0]} ;
desc = new String[]{res[1]};
preco = new String[]{res[3]};
}
lista = (ListView) findViewById(R.id.list);
lista.setAdapter(new dataListAdapter(cod, desc, preco));
Try this solution:
for(int i = 0; i < count; i++) {
String[] res = array.get(i);
cod.add(res[0]) ;
desc.add(res[1]);
preco.add(res[3]);
}
Look at the code carefully:
for(int i = 0; i<count; i++){
String[] res = array.get(i);
cod = new String[]{res[0]} ;
desc = new String[]{res[1]};
preco = new String[]{res[3]} ;
}
lista=(ListView)findViewById(R.id.list);
lista.setAdapter(new dataListAdapter(cod, desc, preco));
Here, the varables cod, desc and preco are created every time in the loop. So they are the last one at the end.
You can try like as follows:
String[] cod = new String[count];
String[] desc = new String[count];
String[] preco = new String[count];
for(int i = 0; i<count; i++){
String[] res = array.get(i);
cod[i] = res[0] ;
desc[i] = res[1];
preco[i] = res[3] ;
}
i have this piece of code
ImageIcon[] Image = {
new ImageIcon("../KingGame/src/game/img/1.gif"),
new ImageIcon("../KingGame/src/game/img/2.gif"),
new ImageIcon("../KingGame/src/game/img/3.gif"),
new ImageIcon("../KingGame/src/game/img/4.gif"),
new ImageIcon("../KingGame/src/game/img/5.gif"),
new ImageIcon("../KingGame/src/game/img/6.gif"),
new ImageIcon("../KingGame/src/game/img/7.gif"),
new ImageIcon("../KingGame/src/game/img/8.gif"),
new ImageIcon("../KingGame/src/game/img/9.gif"),
};
an d i tried with the code below replace the script above
ImageIcon image[] = new ImageIcon[9];
for (int i = 1; i < image.length; i++) {
new ImageIcon("../KingGame/src/game/img/"+i+".gif");
}
but the result is...any image is loaded. what is the error?
thanks
You forgot to put new images into array:
image[i] = new ImageIcon("../KingGame/src/game/img/"+i+".gif");
Now it does the same thing as your old code.
It should be
for (int i = 0; i < image.length; i++) {
image[i] =new ImageIcon("../KingGame/src/game/img/"+(i+1)+".gif");
}