How do I print elements in an ArrayList without the null? - arraylist

This is what I tried doing..
public void printArray()
{
for(int i = 0; i<size; i++)
{
System.out.println("arr[" + i + "] = " + arr[i]);
}
}
this is what I get:
-arr[0] = Papa Bill
-arr[1] = Ali Baba
-arr[2] = Ming the Merciless
-arr[3] = Grandpa
-arr[4] = Tornado
-arr[5] = null
I need it to print with out null...

Just add a check for null in your for loop:
if ( arr[i] != null ){
System.out.println("arr[" + i + "] = " + arr[i]);
}

Related

How to add dapper query parameters in a loop

When add query parameters for dapper in a loop,like this:
if (model.UserGroupId != null && model.UserGroupId.Count>0)
{
var list = model.UserGroupId;
sql += " and ( CHARINDEX(','+#group_id+',',','+mem.group_id+',')>0 ";
paras.Add("group_id", list[0].Trim());
for (var i = 1; i < list.Count(); i++)
{
string data = "#group_id" + i;
sql += " or CHARINDEX('," + data+ ",',','+mem.group_id+',')>0 ";
paras.Add(data, list[i].Trim());
}
sql += " )";
}
it does not report errors, but the query results are incorrect. I can't use dynamic # as a result of searching data. How can I solve this problem?
If I use this it can search correctly:
if (model.UserGroupId != null && model.UserGroupId.Count > 0)
{
var list = model.UserGroupId;
sql += " and ( CHARINDEX(','+#group_id+',',','+mem.group_id+',')>0 ";
paras.Add("group_id", list[0].Trim());
for (var i = 1; i < list.Count(); i++)
{
// string data = "#group_id" + i;
sql += " or CHARINDEX('," + list[i].Trim() + ",',','+mem.group_id+',')>0 ";
// paras.Add(data, list[i].Trim());
}
sql += " )";
}
But it has SQL injection problems.
var list = model.UserGroupId;
sql += " and ( CHARINDEX(','+#group_id+',',','+mem.group_id+',')>0 ";
paras.Add("group_id", list[0].Trim());
for (var i = 1; i < list.Count(); i++)
{
**sql += " or CHARINDEX(','+#group_id"+i+"+',',','+mem.group_id+',')>0 ";
paras.Add("#group_id" + i, list[i].Trim());**
}
sql += " )";
it can search rightly

When I print array same object occurs in array please check.When I print obj then obj.dataArray contains same temp object occurs

Please check if I print obj then in data array same elements gets inserted twice instead of different. If n =2 then the first element gets pushed twice.
genLLEles(n, orgObj, homeSecObj, defaultElementObj, previousElements, socket) {
let that = this;
let arr = [];
let obj = {
dataArray: [],
socketId: socket.USER_ID,
requestId: "/sync#" + socket.id + socket.USER_ID + "#" + new Date().getTime(),
moduleName: "ORG",
action: "INSERT",
userId: socket.USER_ID
};
console.log("\n\ should be oce");
for (let i = 0; i < n; i++) { //n defines count of how many elements to be created
let temp = defaultElementObj; //selected object to insert
let _id = homeSecObj.KEY_VAL + "_TSK:" + defaultElementObj.SUB_KEY_TYPE + "_" + Date.now() + "_" + Math.floor(Math.random() * (10000 - 1)) + 1;
console.log(obj.dataArray.length);
if (obj.dataArray.length == 0) {
temp.CML_PREVIOUS_ID = previousElements.length <= 0 ? "-1" : that.getPreObj(previousElements).KEY_VAL;
temp.CML_NEXT_ID = "1";
temp.ORG_ID = orgObj.ORG_ID;
temp.DEPT_ID = orgObj.DEPT_ID;
temp.KEY_VAL = _id;
temp.CML_ID = _id;
temp.CML_REF_ID = homeSecObj.KEY_VAL;
temp.ACTIVE_STATUS = "1";
temp.CML_IMAGE_PATH = "";
temp.SYNC_PENDING_STATUS = "1";
} else {
obj.dataArray[obj.dataArray.length - 1].CML_NEXT_ID = _id;
console.log("HERE", obj.dataArray[obj.dataArray.length - 1]);
temp.CML_PREVIOUS_ID = obj.dataArray.length < 0 ? "-1" : obj.dataArray[obj.dataArray.length - 1].KEY_VAL;
temp.CML_NEXT_ID = "1";
temp.ORG_ID = orgObj.ORG_ID;
temp.DEPT_ID = orgObj.DEPT_ID;
temp.KEY_VAL = _id;
temp.CML_ID = _id;
temp.CML_REF_ID = homeSecObj.KEY_VAL;
temp.ACTIVE_STATUS = "1";
temp.CML_IMAGE_PATH = "";
temp.SYNC_PENDING_STATUS = "1";
}
obj.dataArray.push(temp);
}
console.log("\n\genLLEles ==> ", obj, "\n\n");
/*return obj;*/
}
from the above code, the values in both the if and else are coming from same parameters. So you are inserting the same values multiple times into the dataArray.

How can i Solve this Code of creating ISO-8385 parser in Java?

Bellow is my code.
for (int i : Utility.getFieldPosList(hexBitmap)) {
IsoValue mtl = this.parserConfig.get(i);
if (mtl == null) {
break;
}
field = parseField(mtl, isoMessageFields);
mtl.setValue(field);
responseMessage.setField(i, mtl);
if(mtl.getIsoType() == IsoType.LLVAR){
isoMessageFields = isoMessageFields.substring(field.length() + 2);
}else if(mtl.getIsoType() == IsoType.LLLVAR){
isoMessageFields = isoMessageFields.substring(field.length() + 3);
}else{
isoMessageFields = isoMessageFields.substring(field.length());
}
sb.append("\nin[ " + i + " ]<" + field + ">");
}
I am not getting the exact value of i, so in this circumstances, I need some suggestion .

QMSClient.SaveCALConfiguration doesn't seem to be working

Can anyone help me understand why this below would not remove named cals. It seems to work fine until the very last line where it does the save. I don't get any exceptions or error messages.
When i look in QV Management console under System>Licenses i still see the ones that were supposed to be removed (Named user CALs)
Client Build Number: 11.20.13314.0
QMSClient Client;
string QMS = "http://localhost:4799/QMS/Service";
Client = new QMSClient("BasicHttpBinding_IQMS", QMS);
string key = Client.GetTimeLimitedServiceKey();
ServiceKeyClientMessageInspector.ServiceKey = key;
List<ServiceInfo> MyQVS = Client.GetServices(ServiceTypes.QlikViewServer);
Client.ClearQVSCache(QVSCacheObjects.All);
CALConfiguration myCALs = Client.GetCALConfiguration(MyQVS[0].ID, CALConfigurationScope.NamedCALs);
List<AssignedNamedCAL> currentNamedCALs = myCALs.NamedCALs.AssignedCALs.ToList();
List<int> indexToRemove = new List<int>();
int cnt = 1;
for (int i = 0; i < currentNamedCALs.Count; i++)
{
if ((currentNamedCALs[i].QuarantinedUntil < System.DateTime.Now)
&& (currentNamedCALs[i].LastUsed < DateTime.Now.AddDays(daysFromToday)))
{
Console.WriteLine("[" + cnt + "] " + currentNamedCALs[i].UserName +
"; Last used: " + currentNamedCALs[i].LastUsed);
indexToRemove.Add(i);
cnt++;
}
}
Console.WriteLine();
for (int i = indexToRemove.Count; i > 0; i--)
{
if (currentNamedCALs[indexToRemove[i - 1]] != null)
{
currentNamedCALs.RemoveAt(indexToRemove[i - 1]);
}
}
Console.WriteLine("\nDone");
myCALs.NamedCALs.AssignedCALs = currentNamedCALs;
Client.SaveCALConfiguration(myCALs);

Quick help turning sum into mean

I was helped earlier in creating this code that would create a histogram of a randomint. Everything looks good except I accidently had the output as a sum instead of a mean of all the numbers that were randomly chosen.I dont want to mess anything up so I was just going to ask, How can I convert this sum into a mean output instead?
import java.util.Random;
class Assignment4
{
public static void main(String[] args)
{
Random r = new Random();
int sum = 0;
int[] bins = new int[10];
for(int i = 0; i < 100; i++)
{
int randomint = 1 + r.nextInt(10);
sum += randomint;
bins[randomint-1]++;
//System.out.print(randomint + ", ");
}
System.out.println("Sum = " + sum);
System.out.println("Data shown below: ");
for (int i = 0; i < bins.length; i++)
{
int binvalue = bins[i];
System.out.print((i+1) + ": ");
for(int j = 0; j < binvalue; j++)
{
System.out.print('*');
}
System.out.println(" (" + binvalue + ")");
}
}
}
Never mind figured it out.... just turned System.out.println("Sum = " + sum); into System.out.println("Mean = " + sum/100);