Why indexOf doesn't work as expected? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have labels which is a list of String and is equal to [0,1]. However the following line returns -1 while I expect it to return 1. Any idea what could be wrong or missing?
definition:
maxLabel is an integer initially set to -1.
public List labels;
where the unexpected result happens, while maxLabel is equal to 1:
int maxLabelIndex=labels.indexOf(maxLabel);

indexOf returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null
In your case the index of element "1" is 1, thats why you will get 1. BUT if you will pass int maxLabel = 1; notice that maxLabel is declared as type int then you will get -1 because Collection of strings doesn't contains an object of type Integer.
Basically indexOf method take as parameter o of type Object. Whenever you pass a different Object type (e.g. you declared your List of type String and you are checking for different types like Integer,Double,int,long, etc.), you can expect to get -1

Related

Is ["abcd"] and [["abcd"]] are same? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have used java ArrayList when I inserted one element in the list and i have converted the validatable response to array list when I used assert equals it is showing that both are different
like one is [abcd] other is [[abcd]]
Validatable response = given().spec(request).filter(new ErrorLoggingFilter(errorPrintStream)).pathParams("","").when.post(endpoint).then()
the response is of the form ArrayList when I printed that It came of the form [[abcd]]
To my knowledge, these two are different things
["abcd"] this means an array has one string "abcd" element.
[["abcd"]] this means an array has one array ["abcd"] element.
Yes, ["abcd"] and [["abcd"]] are completely different. Let us understand why.
Let us consider an array ["abcd"]. As you can see, it contains only one element i.e. "abcd". So this is an array that contains a single string value. Now for [["abcd"]], the outer array contains another array inside of it and the inner array contains "abcd". Though their ultimate content seem to be same, they are absolutely different. One is a string array (an array that contains a string value) and the other is an array of string arrays.
Absolutely different, a one-dimensional array, a two-dimensional array,in many languages,reference form it, [0] and [0][0]

Why boolean is set false at the beginning and is the code right? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
i would like to know first why Boolean is set to be false at the beginning and the pre-last "else" i ca not understand that condition that code is suppose to differentiate vowels from consonants ??
https://beginnersbook.com/2017/09/java-program-to-check-vowel-and-consonant-using-switch-case/
Please post the Code when you ask a Question and don't just put a link in here.
To answer your Question the Code is correct, just the 2nd ; in
boolean isVowel=false;;
isn't necessary. The boolean is set to false to Show that they assume by Default that the given char is no Vowel.
The switch basically checks if the given char is an a,e,i… and has to check for upper and lower case because they are treated different. If the given char Matches any of the given values the boolean is set to true because the char is a vowel.
The second last else Statement checks the UTF-16 values of the Alphabet, you can cast a char to an int which determines ist value in UTF-16 encoding for the lower cases it is 97-122 and for the upper cases their values are 65-90. If the int value of the char is not in this range the char is not in the Alphabet. You can refer to an ascii table to know which char is equivalent to which int.

CharAt in Java 8 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The charAt isn't working... It's returning the hash code and not the value of a part in a structure.
Ex.: charAt(0) where is '1' is returning 49 and not 1
What Am I able to do?!
It >>is<< working. It is returning that character as a char which (presumably) you are assigning to an int and printing. The numeric value of the ASCII / Unicode codepoint for the character '1' is 49. If you want to print / display this as a character, cast the int to a char. (Or don't assign it to an int in the first place.)
For the record, the hashCode value returned by Character is identical to the character value. Strictly speaking a char doesn't have a hashCode because it is a primitive value, and primitives don't have methods.

Remove elements from arraylist based on a condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an ArrayList whose each element is of type DataType, where DataType is a class:
class DataType{
String dId;
String dType;
String rId;
}
I need to remove all such elements from the list whose rId is equal to any other element's dID.
i.e. if DataType D1 has value of dID as "abc" and DataType D2 has value of rID as "abc", than remove both D1 and D2 from the list.
Could someone please suggest the most appropriate approach for doing this.
The easiest would be to traverse the list once and create a HashMap<String, List<DataType>>.
You will map every object to their dID which forms the primary key.
After that you can iterate over your ArrayList, check the rId of the current object and see if it's in the HashMap. HashMap has O(1) lookup time so this should be a non issue. If the value is present, remove the current value (you're using an Iterator to prevent a ConcurrentModificationException) and remove the objects inside the value-part of the key-value pair as well.
Make sure you have correctly implemented .equals(Object o) and .hashcode().

How to check an array before populating it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Does anyone know how can I populate an array that checks if the positions are filled before filling them, and if they are already filled and if that array position is filled it increments to the next one.
Thanks
Primitive array elements are never empty. For example in an int array all elements will be initialized to 0.
So if you want to check for filled or not, initialize the array elements to a value which it is not going to take. say -1. Then each time you make an entry check for -1. The dummy value initialization is necessary as you can not check for 0, because 0 can be a valid data as well
// Initialization part
int [] arr= new int [17];
for(int i=0;i<arr.length;i++)
{
arr[i]= -1;
}
Hope you will do the checking part yourself
I am assuming (possibly incorrectly) that you either a) want a method that inserts object into the first available (i.e. not null) spot in the array at or past the argument integer or b) want to insert repeatedly into the array using the method in a)
the method in a) would basically be as follows
public <Type> void myInsertMethod(Type[] array,Type item,int position)
{
while (array[pos]!=null)
{pos++;}
array[pos]=item;
}
if you want to do this repeatedly on the same array, just call the method repeatedly, with different positions and items.
You can check if the position you are going to fill it's not null, or empty, or at least the stored value it's different to the one you are going to put
if(array.get(index) != "" && array.get(index) != null) && array.get(index) != newObject){
array.add(newObject);
}
Then, depending on the stored value type, you can ddo some more checks, for example in the cas e of the int, as RookieB says, you can check if the object is different to 0
Hope it helps

Categories

Resources