I want to get each array values separately for calculation [closed] - java

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 4 years ago.
Improve this question
I'm java beginner, This is my code.
int [] arr = {1,3,4,5,5,6};
I want to get each array values separately for calculation. can you please help me.
For example: I want first two digits (array index 0 and 1) only how to get that.

to get the first and second item, do like this:
int [] arr = {1,3,4,5,5,6};
System.out.println("pos 0: "+arr[0]+" pos 1: "+arr[1]);
by doing arr[n-1], you select the 'n'th value in the array.
now, if you want to multiply these first two values..:
int result = arr[0] * arr[1]; // 1 * 3
System.out.println(result) // 3

For example: I want to multiply first two digit (1*3) after that (4*5) like that.. so I want to get these values separately.
To do this,
int [] arr = {1,3,4,5,5,6};
int ans;
for(int i=0;i<6;i+=2){
ans = arr[i]*arr[i+1];
System.out.println(""+ans);
}

Related

Dot product of two HashMaps in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Suppose there are two HashMaps as follows:
HashMap<String, Integer> h1 = [{"a":1}, {"b":2}, {"c":3}];
HashMap<String, Integer> h2 = [{"k": 1}, {"f": 4}, {"g":5}, {"a":10}]
The multiplication is just like a simple vector multiplication, in this case it will return
1*10 + 2*0 + 3*0 = 10.
That is if the keys are same, then only multiply the two respective values.
Result -> It should return an integer.
int result = 0;
for(String s : h1.keySet()){
if(h2.containsKey(s)){
result = result + h2.get(s) * h1.get(s);
}
}

Sorting values off array in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to this site and new to java so pleas help me out. If i have array of positive and negative numbers all are int,
how to sort all the positive ones in a new array and all negative in a other new array.
Asking a question in StackOverflow without any selfstudy on basics would attract downvotes. Kindly take time in doing your research. However to make you understand the way to solve I am providing you with a solution.
Step 1: You have an array of positive and negative values
Integer[] initialArray = new Integer[10];
//This contains the list of all values.
Step 2: Create two ArrayLists to save the negative and positive values in each of these.
ArrayList<Integer> positiveValues = new ArrayList<>();
ArrayList<Integer> negativeValues = new ArrayList<>();
Step 3: Iterate through initialArray and save the respective values in both lists.
for(int i =0; i< initialArray.length; i++) {
if(initialArray[i] < 0) {
negativeValues.add(initialArray[i]);
} else {
positiveValues.add(initialArray[i]);
}
}
Step 4: Now sort the values
Collections.sort(negativeValues);
Collections.sort(positiveValues);
Step 5: If Required If you need in arrays instead of ArrayList,
Integer[] negativeArray = new Integer[negativeValues.size()];
Integer[] positiveArray = new Integer[positiveValues.size()];
And cast these to get the array
I actually think this is a pretty good question. Here is an implementation you can try using Java 8 streams.
Given an integer array full of mixed negative/positive numbers:
int[] a = new int[] { 5, 6, 7, 4, -2, 5, -9, 2 };
One solution could be as follows:
int[] pos = Arrays.stream(a).filter(i -> i >= 0).sorted().toArray();
int[] neg = Arrays.stream(a).filter(i -> i < 0).sorted().toArray();
The pos[] array will have all the positive numbers sorted, and neg[] all the negative numbers sorted. The only thing I really dislike is that a[] is traversed twice, but at least it is short.
First seperate positive and negative numbers. I mean find size of positive numbers and create-set new positive numbers array. Then find size of negative numbers and create-set new negative numbers array.
Then just call sort method on both arrays.
Arrays.sort(array);

Remove random index from an arrayList [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 an ArrayList of 4 items. I need to remove one item randomly and display the updated ArrayList. However my random keeps targeting the second and third element on the array list. As far as I understand my random would count like this: 0 1 2 3. Shouldn't that be enough to cover my 4 elements? Why does it keep targeting the same indexes? I have tried increasing the random number (4) + 1, but that puts me out of bounds.
Random rand = new Random();
Scanner input = new Scanner(System.in);
int numberOfGuests = 4;
ArrayList<String> guestList = new ArrayList<>(4);
System.out.println("Enter 4 guests:");
for(int i = 1; i <=numberOfGuests; i++){
System.out.printf("guest%d: ", i);
guestList.add(input.nextLine());
}
System.out.println("Guest List: " + guestList);
String remove = guestList.remove(rand.nextInt(4));
System.out.printf("%s can't come%n" , remove);
System.out.println("Guest List: " + guestList);
yoy check when you user random number generation the rage is set to length of array minus one such as in your case
String remove = guestList.remove(rand.nextInt(3));

Java program that gets 10 int number and calculates the average with a loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to write a java program that gets 10 int numbers and calculates the average.
My requirements are have 3 loops in the same method, while loop, do-while, and for.
For each version, use a loop to input 10 int numbers from the user and calculate the sum. Then display the average.
I would appreciate that help on this. I am having trouble getting the 10 inputs in a loop.
Thanks!
public static void main(String[] args)
{
int sum = 0;
double average = 0;
// for-loop for 10 integers
for(int x = 1; x <= 10; x++)
{
System.out.println("Enter 10 integers ");
Scanner input=new Scanner(System.in);
}
}
}
First you need to import java.util.Scanner;
second create an instance of the class scanner like Scanner input = new Scanner(System.in);
The loop step:
"The main concept is the same for all the three loops".
You need a loop from 0 to < 10 or from 1 to <=10. Then, you need a variable to store the sum of all the variables entered. Inside the loop you will use the instance you created above to get the 10 numbers such as sum += input.nextInt().
After the loop is finish you divide the sum by 10 and return the outcome.
I hope this is clear.

How can I make all initialized integers (instance varibales) into an Array in Java? [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 8 years ago.
Improve this question
If I have some initialized integers which are instance variables, for example:
int x = 0;
int y = 0;
Is there a way I could create an array with all of the integers without doing:
int[] array = {x, y};
I would like to not have to add the integers into the array manually.
If the variables are initialized to 0, then just create the array with the appropriate size:
int[] array = new int[10];
All elements in the array are initialized to 0.

Categories