0

I'm developing a program which gets school marks and subject name from the website. I want to associate a string with an int array, so that each subject is referred to its marks. Is it possible?

EDIT: I mean something like that

array = 
{"english": [5, 7, 9],
 "history": [4, 8, 6],
 .....
}

2 Answers2

3

You can store it in Map (Collection framework). Below is sample code

    Map<String, Integer[]> aMap = new HashMap<String, Integer[]>();
    aMap.put("Maths", new Integer[]{1,2,3,4});
    Integer[] marks = aMap.get("Maths");
    for(int mark: marks){
        System.out.println(mark);
    }

If you are looking for wider range of method you can refer to ListTable

Abhishek
  • 111
  • 1
  • 3
  • 15
  • Why use `Integer[]` when `int[]` is better, given that you don't want null values anyway in the array? – Andreas Sep 05 '16 at 17:43
3

Try a map of String to a list of Integers. Something like this:

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//create the map like this
Map<String, List<Integer>> studentMarks = new HashMap<String, List<Integer>>();

//put values in the map like this   
studentMarks.put("Student name 1", Arrays.asList(65, 70, 85, 45));
studentMarks.put("Student name 2", Arrays.asList(95, 56, 34, 41));


//retrieve the marks for one student like this  
List<Integer> marksForStudent1 = studentMarks.get("Student name 1");
Tom Hanley
  • 840
  • 9
  • 15