Multiple arrays: Key and value.

In summary, I was able to get at least 2 out of the 3 test outputs but I can't seem to get the last one. I tired taking out the dividing by 2 but then the first test output doesn't work.
  • #1
lypena35
18
0
Hello, I am stuck on this question.The bold part is where I am stuck. I was able to get at least 2 out of the 3 test outputs but I can't seem to get the last one. I tired taking out the dividing by 2 but then the first test output doesn't work. Thank you.

Question:
For any element in keysList with a value greater than 100, print the corresponding value in itemsList, followed by a space. Ex: If keysList = {42, 105, 101, 100} and itemsList = {10, 20, 30, 40}, print:
20 30
Since keysList[1] and keysList[2] have values greater than 100, the value of itemsList[1] and itemsList[2] are printed.
This is my code

Code:
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 = 0;

      keysList[0] = 42;
      keysList[1] = 105;
      keysList[2] = 101;
      keysList[3] = 100;

      itemsList[0] = 10;
      itemsList[1] = 20;
      itemsList[2] = 30;
      itemsList[3] = 40;
[B]
     * for(i=0;i<SIZE_LIST;i++){
         if(keysList[i]>100){
            System.out.print(itemsList[i]/2+itemsList[i]/2+(" "));
           
         }
         
         }*
[/B]
      System.out.println("");

      return;
   }
}
This is the test output.

Testing with keysList ={42, 105, 101, 100} and
itemsList = {10, 20, 30, 40}
Your output: 20 30

Testing with keysList ={99, 100} and
itemsList = {14, -11}
Your output:

✖ Testing with keysList ={101, 312, 541, 120, 199, 114, 107, 221, 987, 876} and
itemsList = {1, 2, 17, 98, 75, 96, 3, 492, -14, -11}
Expected output: 1 2 17 98 75 96 3 492 -14 -11
Your output: 0 2 16 98 74 96 2 492 -14 -10
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Why do you print [m]itemsList/2 + itemsList/2[/m] instead of [m]itemsList[/m]? Where does the problem statement say something about dividing by 2?
 
  • #3
I tired it with just the itemsList but I received a error. I now understand the error must have been with another part of my code. The division was only a temporary fix to answer the first two outputs but it was to specific, so I needed a more generic answer. I retyped my portion of the code in and I used the itemsList as you suggested and it worked this time. Thank you very much for your help. I appreciate it.
 

1. What is a multiple array?

A multiple array is a data structure in which multiple arrays are stored within a single array. Each element of the main array contains another array, known as a sub-array, which can hold its own set of elements. This allows for more complex and organized data storage.

2. What is a key-value pair in a multiple array?

A key-value pair is a set of two elements within a multiple array that are linked together. The key is a unique identifier for a specific value in the array, and the value is the data associated with that key. This allows for easy retrieval and manipulation of data in the array.

3. How are key-value pairs represented in a multiple array?

In a multiple array, key-value pairs are typically represented as sub-arrays, where the first element is the key and the second element is the corresponding value. For example, [ [key1, value1], [key2, value2], [key3, value3] ].

4. How can I access a specific value in a multiple array?

To access a specific value in a multiple array, you can use the key to retrieve the corresponding value. This can be done by looping through the main array and checking the keys in each sub-array until the desired key is found. Alternatively, you can use built-in methods such as filter or find to retrieve the value.

5. What are the advantages of using a multiple array for data storage?

Multiple arrays offer several advantages for data storage, including the ability to store organized and complex data, easy retrieval and manipulation of data using keys, and the ability to easily add or remove data without affecting the structure of the array. They are also useful for storing and accessing data in a hierarchical format.

Similar threads

  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
7
Views
416
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
22
Views
749
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
994
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top