155k views
1 vote
For any element in keysList with a value greater than 50, print the corresponding value in itemsList, followed by a space. Ex: If keysList = {32, 105, 101, 35} and itemsList = {10, 20, 30, 40}, print: 20 30

JAVA

import java.util.Scanner;

public class ArraysKeyValue {
public static void main (String [] args) {

final int SIZE_LIST = 4;
int[] keysList = new int[SIZE_LIST];
int[] itemsList = new int[SIZE_LIST];
int i;

keysList[0] = 13;
keysList[1] = 47;
keysList[2] = 71;
keysList[3] = 59;

itemsList[0] = 12;
itemsList[1] = 36;
itemsList[2] = 72;
itemsList[3] = 54;

User Hritik
by
7.6k points

1 Answer

3 votes

Answer:

import java.util.Scanner; //mentioned in the question.

public class ArraysKeyValue { //mentioned in the question.

public static void main (String [] args) { //mentioned in the question.

final int SIZE_LIST = 4; //mentioned in the question.

int[] keysList = new int[SIZE_LIST]; //mentioned in the question.

int[] itemsList = new int[SIZE_LIST]; //mentioned in the question.

int i; //mentioned in the question.

keysList[0] = 13; //mentioned in the question.

keysList[1] = 47; //mentioned in the question.

keysList[2] = 71; //mentioned in the question.

keysList[3] = 59; //mentioned in the question.

itemsList[0] = 12; //mentioned in the question.

itemsList[1] = 36; //mentioned in the question.

itemsList[2] = 72; //mentioned in the question.

itemsList[3] = 54;//mentioned in the question.

// other line to complete the solution is as follows--

for(i=0;i<(keysList.length);i++) //Loop to access all the element of keysList array variable.

{// open for loop braces

if(keysList[i]>50) // compare the value of keysList array variable with 50.

System.out.println(itemsList[i]); // print the value

}// close for loop braces.

}// close main function braces.

} //close the class braces.

Output:

72

54

Step-by-step explanation:

In the solution part--

  1. One loop is placed which moves from 0 to (size-1) of the 'keyslist' array.
  2. Then the 'if' statement is used to check the element of 'keyslist' array, which is greater than 50 or not.
  3. If it is greater, then print the element of item_list array of 'i' index which is also the index of greater value of keyslist array element.

User Hudson Pereira
by
8.5k points