Trouble obtaining average to an array [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to get an average of an array and I found this code during my research.
int myArray[] = {1,2,3};
Arrays.stream(myArray).average();
System.out.println(Arrays.toString(myArray));
When I run it, the results come up as
[1, 2, 3]
I'm new at java and I feel like there's something obvious that I'm not seeing.

You are printing the original Array.
To print the calculated array add the next line:
System.out.println(Arrays.stream(myArray).average().getAsDouble());
Output:
2.0

You are just printing only the array, Instead, you can do this.
int myArray[] = {1,2,3};
System.out.println(Arrays.toString(Arrays.stream(myArray).average().getAsDouble()));
OR
int myArray[] = { 1, 2, 3 };
OptionalDouble ans = Arrays.stream(myArray).average();
System.out.println(ans.getAsDouble());
Here,OptionalDouble is a container object which may or may not contain
a double value. If a value is present, isPresent() will return true
and getAsDouble() will return the value.

Related

How come for Integer arrays, as in Integer arr[] = new Integer[], you can't change their value, you can only set them? [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 2 years ago.
Improve this question
I was doing a problem in Codeforces and had to use an Integer arr[] = new Integer[200001]. I wanted to store 200001 counters for their index values(for example, if I got 19 then arr[19] would be the number of 19s that appeared in the array). However, when I went to do arr[num]++ or arr[num] += 1, it would not work. Only when you set arr[num] to some number does it work. Is there a way to get around that?
It wouldn't work because you haven't initialized each element of the array, you just have an Integer array with null references.
you should first do:
for(int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
The easiest way is to use an int array instead of an Integer array.

Set of Arraylists in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Set<ArrayList<Integer>> hs = new HashSet<ArrayList<Integer>>();
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
hs.add(arr);
ArrayList<Integer> arr1 = new ArrayList<Integer>();
arr1.add(4);
arr1.add(3);
arr1.add(2);
arr1.add(1);
hs.add(arr1);
System.out.println(hs.size());
The output I get is 2. I want to get 1 as both the arrayLists have the same elements. How can I achieve this?
In the second block, after creating arr1, you add the number 1 to 3 to arr again. Obviously, an ArrayList with 8 elements isn't equal to an empty ArrayList, so you'd have two members in the HashSet. If you fix the code to add the same elements to arr1, you'll get a HashSet with a size of 1.
In the code you pasted, you are adding 1, 2, 3, and 4 to the first list (arr) twice, and adding nothing to the second list (arr1). Thus arr1.equals(arr) is going to be false.
Fix the typo in your code so that arr1 contains the same elements as arr, and you will achieve your goal.
Even if the contents are same, both arraylists are different objects. You are bound to see 2 elements in the set.
Its like having two employees with exactly the same name.
Not sure if you can implement equals method for arraylists.

Integer.parseInt giving java.lang.NumberFormatException in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a character array as
char[] bitsString = new char[16];
bitsString = {'1','1','1','1','1','1','0','1','1','1','1','0','0','1','1','0'};
Then I converted it to the corresponding integer as follows:
int givenNumber = Integer.parseInt(new String(bitsString), 2);
The above logic works fine when number bitstring array length is less than 10. But when it is increased to 11 or more, it is showing me java.lang.NumberFormatException why ?
After some hit and trials I found that, it was the case since the 16 bit value is crossing the Integer limit.
But when it is changed from int to long
long givenNumber = Long.parseLong(new String(bitsString), 2);
it is working perfectly fine. Since in this case long has 64 bits length.

I am getting a "not a statement" error on a for each loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)

Best algorithm to remove elements between 2 given indices [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
So I'm trying to learn algorithm efficiency. I know how to remove one element from the array, but not sure how to remove between two indices. Lets pretend that the
list = {1,2,3,4,5,6,7,8,9,10}, and we call the removeBetween method with arguments:
removeBetween(2, 6);
public void removeBetween(int FirstIndex, int LastIndex)
{
}
A general algorithmic direction you can follow :
To remove all numbers between 2 given indices, say (FirstIndex, LastIndex):
Copy all elements from index 0 to FirstIndex to a result array.
Next copy all elements from LastIndex to Array.lenght()-1 indices to the same result array above.
Return result.
so if you have your items in an array list you can do something like:
ArrayList<Whatever object is> newElements = yourlist.sublist( 0, firstIndex );
newElements.addAll( yourlist.subList( LastIndex + 1, yourlist.size() ) ;

Categories

Resources