HashMap<String, ArrayList>, Appending ArrayList with new values based on the Key
Date : March 29 2020, 07:55 AM
|
appending an array of string to arraylist of type Integer in Java
Tag : java , By : Helpful Dude
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have an ArrayList called nodes. I want to assign elements from array[] to elements in ArrayList. Such that 1st element in arraylist will have first element in array as its property.. and so on. However, there are only 6 elements in array and thus for 7th element its again first element from array[]. Array is of type Integer. , Sounds like you need a Node class with a Map to store preferences.class Node {
Integer id;
Map<String, Integer> interestPreferences;
public Node(Integer id) {
this.id = id;
this.interestPreferences = new HashMap<String, Integer>();
}
void setPreference(String key, Integer value) {
interestPreferences.put(key, value);
}
Interest getPreference(String key) {
return interestPreferences.get(key);
}
}
public static List<Node> nodes = new ArrayList<Node>();
public static String[] Interest = new String[] {"I1","I2","I3","I4","I5","I6"};
public static void main(String[] args){
System.out.println("Enter number of nodes");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<=n;i++) {
Node node = new Node(i);
node.setPreference("I1", 10);
node.setPreference("I2", 8);
//....
node.setPreference("I6", 2);
nodes.add(node);
}
System.out.println(nodes);
}
|
Tag : java , By : user165781
Date : March 29 2020, 07:55 AM
I wish did fix the issue. StringBuilder is more efficient in terms of memory in most cases. With ArrayList you keep all of the Strings in memory, with StringBuilder you keep only the characters in a char array, because the actual Strings will be GCed. Thus, if you have 10000 Strings you have a difference of memory of 10000 * difference in memory between a String and it's corresponding characters (which are objects in String class itself, like the lock, length, hash and eventually others).
|
Arraylist of String arrays not appending?
Tag : java , By : user183275
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have created 3 arraylist which have a array of another type: , With this code Log.e("tttttt", Arrays.toString(arrayListString.get(0)));
Log.e("ppppppp", Arrays.toString(arrayListInt.get(0)));
Log.e("mmmmmm", Arrays.toString(arrayListInt.get(1)));
Log.e("dddddd", Arrays.toString(arrayListInt.get(2)));
Log.e("iiiiii", Arrays.toString(arrayListString.get(1)));
Log.e("tttttt", Arrays.toString(arrayListString.get(2)));
public class ApiData {
public String[] title, image;
int[] price, mrp, discount;
}
private List<ApiData> arrayListData = new ArrayList<ApiData>();
@Override
public void run() {
ApiData data = new ApiData();
data.title = Arrays.copyOf(product.getTitle(), product.getTitle().length);
...
arrayListData.add(data);
|
ItemArray to string with format is invalid
Tag : chash , By : RyanMcG
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The ItemArray is an object[]. Object.ToString has no parameter. I guess it's actually a currency value. Then use the DataRow.Field extension method to cast it: foreach (DataRow dr in ret.Rows)
{
DataRow row = dt.Rows.Add();
for (int j = 0; j < dr.ItemArray.Length; j++)
{
if (j == 14)
row[j] = dr.Field<decimal>(j).ToString("C2"); // use the correct type
else
row[j] = dr.ItemArray[j];
}
}
foreach (DataRow dr in ret.Rows)
{
DataRow row = dt.Rows.Add();
foreach(DataColumn col in ret.Columns)
{
if (col.Ordinal == 14)
row.SetField(col.Ordinal, dr.Field<decimal>(col).ToString("C2")); // use the correct type
else
row.SetField(col.Ordinal, dr[col]);
}
}
|