Sorting Arrays In Java soicanpost - java

my question is how would I sort the arrayofnames and arrayofdownloads so they're in ascending order and each name matches with it corresponding number of downloads. i've been trying for 4 hours and i can't seem to wrap my head around it
thanks
import java.util.*;
import java.util.stream.*;
public class short6
{
public static void main(String[] args)
{
String[] arrayofnames = new String[4];
int[] arrayofdownloads = new int[4];
printmessage(arrayofnames, arrayofdownloads);
details(arrayofnames, arrayofdownloads);
System.exit(0);
}
public static void printmessage(String[] arrayofnames, int[] arrayofdownloads)
{
Scanner scanner = new Scanner(System.in);
int totalDownloads = 0;
for (int i = 0; i < arrayofnames.length; i++)
{
System.out.println("What is track " + (i + 1));
arrayofnames[i] = scanner.nextLine();
System.out.println("How many thousands of times has it been downloaded? ");
arrayofdownloads[i] = Integer.parseInt(scanner.nextLine());
}
Arrays.sort(arrayofnames);
Arrays.sort(arrayofdownloads);
System.out.println("The track downloaded the most is " + arrayofdownloads[0]+".");
}
public static void details(String[] arrayofnames, int[] arrayofdownloads)
{
int totaldownloads = IntStream.of(arrayofdownloads).sum();
System.out.println("The track downloaded the most is " + arrayofdownloads[0]+".");
System.out.println("The total number of downloads of these 4 tracks was " + totaldownloads * 1000 +".");
System.out.println("\nThe details of the downloads are");
for (int i = 1; i < arrayofnames.length; i++)
{
System.out.println(arrayofnames[i]);
System.out.println((arrayofdownloads[i]));
}
}
}

I'd start creating a Song (e.g.) class that contains both the song name, and the number of downloads:
public class Song {
private String name;
private int downloads;
public Song(String name, int downloads) {
this.name = name;
this.downloads = downloads;
}
public String getName() {
return this.name;
}
public int getDownloads() {
return this.downloads;
}
}
And then, create an array of songs:
Song[] arrayOfSongs = new Song[4];
And load it from the input:
arrayOfSongs[i] = new Song(scanner.nextLine(), Integer.parseInt(scanner.nextLine()));
Now you just need to sort it using a comparator:
Arrays.sort(arrayOfSongs, new Comparator<Song> () {
public int compare(Song o1, Song o2) {
// Sorting by name
return o1.getName().compareTo(o2.getName());
}
});
Or, as #Benoit has said in the comments, it would be even easier this way (Java 8 or up):
Arrays.sort(arrayOfSongs, Comparator.comparing(Song::getName));
And your done, just printing the objects (a toString method can be helpful here) you have the information sorted.
Edit: to read the input writing the questions, you just need to store the values in variables e.g.
System.out.println("What is track " + (i + 1));
String songName = scanner.nextLine();
System.out.println("How many thousands of times has it been downloaded? ");
int songDownloads = Integer.parseInt(scanner.nextLine());
arrayOfSongs[i] = new Song(songName, songDownloads);
Or you can just implement setter methods in the Song class and create a constructor with no parameters, so you can set the values as you are reading them.

thanks for the reply, so I've figured that i can use a for loop to ask the questions for song name and number of downloads. what im finding hard is putting their response in each array from 1-4 and saving dong name in the correct field of the song record and same for number of downloads?
public static void printmessage(song[] arrayOfSongs)
{
Scanner scanner = new Scanner(System.in);
int totalDownloads = 0;
for (int i = 0; i < arrayOfSongs.length; i++)
{
System.out.println("What is track " + (i + 1));
// store in array x name field
System.out.println("How many downloads does this track have? ");
//store in array x downloads field
}

Related

How to call a function once for all objects?

I'm having trouble understanding how I can call a function with multiple objects only once (within a for loop).
I'm trying to write a code where information of the objects should be passed to another function.
Take the following program for example where, instead of once, the function is being called multiple times.
import java.util.Scanner;
class Attraction {
String name;
int open;
}
public class attractionGuide {
public static void main(String[] args) {
attractionDetails();
System.exit(0);
}
public static Attraction createAttraction(String attractionName, int openingTime) {
Attraction a = new Attraction();
a.name = attractionName;
a.open = openingTime;
return a;
}
public static void attractionDetails() {
Attraction TheEdenProject = createAttraction("The Eden Project", 9);
Attraction LondonZoo = createAttraction("London Zoo", 10);
Attraction TateModern = createAttraction("Tate Modern", 10);
attractionInfo(TheEdenProject);
attractionInfo(LondonZoo);
attractionInfo(TateModern);
// This is where the problem is^
}
public static Attraction attractionInfo(Attraction a) {
Scanner scanner1 = new Scanner(System.in);
System.out.print("Welcome. How many attractions do you need to know about? ");
final int howMany = scanner1.nextInt();
for (int i = 1; i <= howMany; i++) {
System.out.print("\nName of attraction number number " + i + "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
if (attraction_name.equalsIgnoreCase(a.name)) {
System.out.println(a.name + " opens at " + a.open + "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
return a;
}
}
Example output:
Welcome. How many attractions do you need to know about? 3
Name of attraction number number 1?: the eden project
The Eden Project opens at 9am.
Name of attraction number number 2?: tate modern
I have no information about that attraction.
Name of attraction number number 3?: london zoo
I have no information about that attraction.
Welcome. How many attractions do you need to know about?
It seems since the function is being called three times within the loop, it will only take one argument at a time, whereas the desired output should look something like this:
Expected output:
Welcome. How many attractions do you need to know about? 3
Name of attraction number number 1?: the eden project
The Eden Project opens at 9am.
Name of attraction number number 2?: tate modern
Tate Modern opens at 10am.
Name of attraction number number 3?: london zoo
London Zoo opens at 10am.
so how do I go about calling the function once so it takes all the arguments at once?
Any help is appreciated. Thanks.
Create a List of attractions as in
List<Attraction> attractions = new ArrayList<>();
Add each attraction to the list:
attractions.add(TheEdenProject);
attractions.add(LondonZoo);
attractions.add(TateModern);
Pass the List to attractionInfo
attractionInfo(attractions);
Define attractionInfo as
// Didn't see why you needed to return Attraction
public static void attractionInfo(List<Attraction> allKnown)
And use it as such:
Scanner scanner1 = new Scanner(System.in);
System.out.print("Welcome. How many attractions do you need to know about? ");
final int howMany = scanner1.nextInt();
for (int i = 1; i <= howMany; i++) {
System.out.print("\nName of attraction number number " + i + "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
Attraction lookup = createAttraction(attraction_name,0);
if (allKnown.contains(lookup)) {
Attraction ofInterest = allKnown.get(allKnown.indexOf(lookup));
System.out.println(ofInterest.name + " opens at " + ofInterest.open + "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
The contains assumes the equals of Attraction is implemented as the desired name comparison. If you need help with that just ask. Or search on [java] equals in SO.
You need some collection to iterate over, like a List.
List<Attraction> attractions = Arrays.asList(
createAttraction("The Eden Project", 9),
createAttraction("London Zoo", 10),
createAttraction("Tate Modern", 10)
);
for (Attraction attraction : attractions) {
attractionInfo(attraction);
}
Or, more advanced
Stream.of(
createAttraction("The Eden Project", 9),
createAttraction("London Zoo", 10),
createAttraction("Tate Modern", 10)
)
.forEach(attractionGuide::attractionInfo);
The problem is you are calling your loop three times.
attractionInfo(TheEdenProject); // This creates a loop
attractionInfo(LondonZoo); // Ditto
attractionInfo(TateModern); // Ditto
If you want to loop once, you must call the loop only once. Try creating the loop and having it call the method that checks the attraction.
// Closer to main()
Attraction a;
for (int i = 1; i <= howMany; i++) {
System.out.print("\nName of attraction number number " + i + "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
// Add this
if ( (a = attractionInfo(a.name)) != null ) {
System.out.println(a.name + " opens at " + a.open + "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
Pass an array or collection to iterate over. the simplest change is below, but you could also use a Map to get the info by name rather than iterating over the entire collection .
import java.util.Scanner;
public class AttractionGuide {
static class Attraction {
String name;
int open;
Attraction(String name, int open) {
this.name = name;
this.open = open;
}
}
public static void main(String[] args) {
attractionDetails();
System.exit(0);
}
public static void attractionDetails() {
Attraction TheEdenProject = new Attraction("The Eden Project", 9);
Attraction LondonZoo = new Attraction("London Zoo", 10);
Attraction TateModern = new Attraction("Tate Modern", 10);
queryAttractionInfo(TheEdenProject, LondonZoo, TateModern);
}
public static void queryAttractionInfo(Attraction ... attractions) {
Scanner scanner1 = new Scanner(System.in);
System.out.print("Welcome. How many attractions do you need to know about? ");
final int howMany = scanner1.nextInt();
for (int i = 1; i <= howMany; i++) {
System.out.print("\nName of attraction number number " + i + "?: ");
Scanner scanner2 = new Scanner(System.in);
String attraction_name = scanner2.nextLine();
boolean found = false;
for (Attraction a : attractions) {
if (attraction_name.equalsIgnoreCase(a.name)) {
System.out.println(a.name + " opens at " + a.open + "am.");
break;
}
}
if (!found) {
System.out.println("I have no information about that attraction.");
}
}
}
}
well, there so many answers.
i think this is your mean:
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Welcome. Enter attractions count:");
int count = scanner.nextInt();
System.out.println("count: " + count);
attractionInfo(count, "Jim", 10);
attractionInfo(count, "Tom", 2);
attractionInfo(count, "Jack", 4);
scanner.close();
}
public static void attractionInfo(int count, String name, int open) {
for (int i = 1; i <= count; i++) {
System.out.print("Enter a name:");
Scanner scanner = new Scanner(System.in);
String attraction_name = scanner.nextLine();
if (attraction_name.equalsIgnoreCase(name)) {
System.out.println( name + " opens at " + open + "am.");
} else {
System.out.println("I have no information about that attraction.");
}
}
System.out.println("=================");
}

How to access array from constructor

I just started with Java a few weeks ago and today I've tried to write a program which is able to calculate the average IQ of numbers the user can input. I've written two classes, IQ and IQTester (IQTester = Main only). Now my problem is, whenever I want to calculate something in method compute() (e.g. the average of the array) the whole array is empty. Does anybody know how I can "pass" the array from the constructor to the method compute()?
package IQProgramm;
public class IQ {
private int values[] = new int[10];
private double average;
public IQ(String numbers) {
this.values = values;
String[] values = numbers.split(";");
System.out.println("Calculate: ");
System.out.println("You've input the following numbers: ");
for (int i = 0; i < values.length; ++i) {
System.out.print(values[i] + " ");
}
System.out.println("\n");
}
public void compute() {
for (int i = 0; i < values.length; ++i) {
System.out.println(values[i]);
}
}
}
package IQProgramm;
import java.util.Scanner;
public class IQTester {
public static void main(String[] args) {
Scanner readIQ = new Scanner(System.in);
System.out.println("Please enter your numbers: ");
String numbers = readIQ.nextLine();
IQ iq = new IQ(numbers);
iq.compute();
}
}
You have 2 different arrays named values, that's why it doesn't work well.
The first defined here String[] values = numbers.split(";"); is visible only in the constructor. If you want to set the value of the one that is available in the rest of the IQ class (private int values[] = new int[10];), you need to edit this one by using
this.values[i] = Integer.parseInt(values[i])
this refers to the variable values of the class IQ.
It is a good practice not to have 2 values with same name. You can change String[] values name to valuesStr for example.
Constructor with the fix:
public IQ(String numbers) {
String[] valuesStr = numbers.split(";");
System.out.println("Calculate: ");
System.out.println("You've input the following numbers: ");
for (int i = 0; i < valuesStr.length; ++i) {
this.values[i] = Integer.parseInt(valueStr[i])
System.println(this.values[i]+" ");
}
System.out.println("\n");
}

Vector in java always overwriting everything with the last value

I'm a beginner in java and I have the following exercise to solve:
Read a set of sports lottery bets to an unspecified number of players. After that, read the template, compare the result and display a message.
Bet Class: must contain the player's name and an integer vector with thirteen positions to store the bet: 1 – winner of team A, 2 – winner of team B, and 0 for draw. Create the necessary constructor methods and the toString method.
ReadBets Class: This class must have an attribute that is a vector of objects of the Bet class. In addition, this class must have a method to read the person's name and their sports lottery game. Create the necessary constructor methods and the toString method.
ReadTemplate Class: This class should read the correct answers from the sports lottery game and store the result in an integer vector. Create the necessary constructor methods and the toString method.
GenerateResult Class: this class must compare the players' bets with the result of the template and, if you have 6 correct answers, show the winner's name, his game and the message: “WINNER, CONGRATULATIONS”. Otherwise, show the player name and the message “Try again, this time you didn't win”.
Main Class: implement in this class the control and execution flow for this problem creating the necessary objects to solve this class.
There is a complicating factor for me which is that each object bet is composed of a string name with a vector with the bets.
I made a solution to the problem that already reads validating only the allowed values (0,1,2), reads the template and compares.
However, I noticed that the last name typed and the last bets are always being recorded in the vector. In all positions.
I'm hours assembling and disassembling the code and I can't find a way to solve it.
I read that it may have to do with the new but I didn't understand how to solve it because at each position of the vector of objects I need to give a new to create the object with the internal vector that will store that player's bets.
Below are my classes:
MAIN:
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Main {
public void execute(){}
static Scanner input = new Scanner (System.in);
public static void main(String[] args) {
//ReadBet a = new ReadBet();
V_Bets a = new V_Bets();
System.out.println("Enter the total bets that will be placed");
a.totalBets(input.nextInt());
a.Data entry();
a.Data output();
ReadTemplate g = new ReadTemplate();
g.getTemplate();
GenerateResult gr = new GenerateResult();
gr.generateResult();
}
}
BETS:
import java.util.Arrays;
public class Bet {
private static String name;
public static int[] Bet vector =new int [6];
/*public String getName(){
return this.name;
}*/
public static String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
#Override
public String toString() {
return "Bet [name=" + name + ",Betvector=" + Arrays.toString(Betvector) + "]";
}
public int getBet(int i) {
return this.vector Bets[i];
}
public void setBets(int[] bets) {
this.vector Bets = bets;
}
public int[] getCloneArray() {
returnVectorBets.clone();
}
}
V_BETS
import java.util.Scanner;
public class V_Bets {
Input Scanner = new Scanner (System.in);
private int size;
public static Total BetBets[] = new Bet[100];
//total Bets[j] = new Bet(); //Creating a bet object for each registration - bet object contains name and bet vector of that bettor
private Bet bets = new Bet();
int i=0;
int j=0;
public void dataentry() {
for(j=0;j<size;j++) { //for external - from J that controls the total bets that will be registered
totalBets[j] = new Bet(); //Creating a bet object for each registration - bet object contains name and bet vector of that bettor
System.out.println("Enter the name of the Player:");
totalStakes[j].setName(input.next()); //setting the player's name
// loop to set the guesses
System.out.println("Please enter guesses for each Sports Lottery game");
System.out.println("1 - Winner Team A, 2 - Winner Team B and 0 - Tie");
for ( i=0;i<bets.vetorApostas.length;i++) // receive the bet until it reaches 6
{
System.out.println("Game "+i +":");
totalStakes[j].vectorStakes[i] = input.nextInt(); // receive the user's bets
while (totalApostas[j].vectorApostas[i] < 0 ) // if the user enters a negative number it says to try again
{
System.err.println("Negative Number, try again:");
totalBets[j].vectorBets[i]= entry.nextInt();
}
if (totalApostas[j].vetorApostas[i] > 2) // if the number is greater than 2 it says to try again
{
while (totalBets[j].vectorBets[i] > 2 ) {
System.err.println("Please enter numbers between 0 and 2");
totalBets[j].vectorBets[i]= entry.nextInt();
}
}
}
}
}
/*public void dataoutput() {
//System.out.println("The player's name is:"+total Bets[i].getName());
System.out.println("Your Bet:");
for ( i=0;i < Bet.vectorBeats.length; i++){
System.out.print(+TotalStakes[i].vectorStakes[i].getStakes(i)+ " ");
}
}
public void dataoutput() {
System.out.println("Your Bet::");
for (j=0; j<totalBets[i].vectorBets.length; j++) {
System.err.print("Player "+Total Bets[j].getName()+" ");
for (i = 0; i < totalBets[i].vectorBets.length; i++) {
System.err.print(total Bets[j].vector Bets[i] + " ");
}
}
}*/
public void totalOfBets(int size) {
this.size = size;
}
public int getSize() {
return size;
}
}
TEMPLATE:
public class Template {
//private String name;
public static int[]vectorTemplate =new int [6];
public int getBet(int i) {
return this.vectorTemplate[i];
}
public void setBets(int[] bets) {
this.vectorTemplate = bets;
}
public int getTemplate(int i) {
return this.vectorTemplate[i];
}
public void setTemplate(int[] bets) {
this.vectorTemplate = bets;
}
}
READ TEMPLATE:
import java.util.Arrays;
import java.util.Scanner;
public class ReadTemplate {
private Template template = new Template();
Input Scanner = new Scanner (System.in);
int guesses;
int counter=0;
int j;
int x;
int i;
public void getTemplate() {
System.out.println(" ");
for ( j=0;j<6;j++) // Receive numbers until it reaches 6
{
System.out.println("Now Enter Game Template: "+j);
template.vectorTemplate[j]= entry.nextInt(); // Receive user numbers
while (template.vetorTemplate[j] < 0 ) // If you enter a negative number, ask the user to type again
{
System.err.println("Negative Number, try again:");
template.vectorTemplate[j]=input.nextInt();
}
if (template.vectorTemplate[j] > 2) // if the number is greater than 2 ask again
{
while (template.vectorTemplate[j] > 2 )
{
System.err.println("Please enter numbers between 0 and 2");
template.vectorTemplate[j]=input.nextInt();
}
}
}
//printing the template
System.out.println("Template:");
for (i = 0; i < template.vectorTemplate.length; i++) {
System.out.print(template.vectorTemplate[i] + " ");
}
}
}
GENERATE RESULTS:
public class GenerateResult extends ReadTemplate {
private Bet bets = new Bet();
private Template template = new Template();
//private v_bets v_bets = new v_bets();
private int size;
public void generateResult() {
//Checking if it's right
int x;
V_Bets a = new V_Bets();
for(j=0;j<2;j++) {
int i=0;
int counter=0;
for (x = 0; x < template.vectorTemplate.length; x++) {
if (a.totalStakes[j].vectorStakes[i] == template.vectorTemplate[x]) {
counter++;
}
i++;
}
System.out.println(" ");
System.out.println("Counter of Equals! "+counter);
if (counter <= 5){
System.out.println("Player "+a.Total Bets[j].getName());
System.out.println("Try again, this time you won");
}
else if (counter == 6){
System.out.println("Player "+a.Total Bets[j].getName());
System.out.println("WINNER,CONGRATULATIONS!");
}
}
}
public void totalOfBets(int size) {
this.size = size;
}
}
I am immensely grateful to anyone who can help understand where I went wrong and how to evolve.

Can I search for values in two different type of arrays with just one type of variable?

I'm quite new to Java and I've been asked to create a program in which the user is able to input two values and store them in separate arrays. The two values I'm asking the user are name and cell number, then I must allow the user to search by typing either a name or a cell number and return the corresponding name or cell number. I made it possible to input the values and search within them by number but when I try searching by name I get this error :
Exception in thread "main" java.lang.NumberFormatException: For input string: "B"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
This is my code:
import java.util.Scanner;
public class HW {
static Scanner sc = new Scanner(System.in);
private static int i, x = 2;
static String names[] = new String[x];
static int numbers[] = new int[x];
public static void main(String[] args) {
Input();
Compare();
}
public static void Input() {
System.out.println("Enter a name followed by the persons number");
while (i < x) {
System.out.println("NAME: ");
names[i] = sc.next();
System.out.println("NUMBER: ");
numbers[i] = sc.nextInt();
i++;
}
}
public static void Compare() {
System.out.println("=======SEARCH=======\nSEARCH CRITERIA: ");
var temp = sc.next();
System.out.println("NAME\tNUMBER");
for (i = 0; i < numbers.length; i++)
if ((names[i].equals(temp)) || (numbers[i] == Integer.parseInt(temp.trim()))) {
System.out.println(names[i] + "\t" + numbers[i]);
}
}
}
Thanks! :)
Looking at your problem statement it doesn't seem like you need to do any additional processing on numbers. Hence, even if you store the number as a string it should be fine in this case.
Hence after getting a user search criteria, you could do a simple string search within both arrays.
Hope this helps :)
First of all, the highest number that can be represented as an int in Java is 2147483647 (214-748-3647). This clearly will not be able to hold a high enough number to accommodate any phone number. To address this issue and also fix your main error, I would suggest storing the numbers as a string instead. Here's my solution:
import java.util.Scanner;
public class HW {
static Scanner sc = new Scanner(System.in);
private static int x = 2;
static String names[] = new String[x];
static String numbers[] = new String[x];
public static void main(String[] args) {
input();
compare();
}
public static void input() {
System.out.println("Enter a name followed by the persons number");
for (int i = 0; i < x; i++) {
System.out.println("NAME: ");
names[i] = sc.next();
System.out.println("NUMBER: ");
numbers[i] = sc.next();
i++;
}
}
public static void compare() {
System.out.println("=======SEARCH=======\nSEARCH CRITERIA: ");
String temp = sc.next();
System.out.println("NAME\tNUMBER");
for (int i = 0; i < numbers.length; i++) {
if ((names[i].equals(temp)) || numbers[i].equals(temp)) {
System.out.println(names[i] + "\t" + numbers[i]);
}
}
System.out.println("===END OF SEARCH====")
}
}
Please also note that I un-defined your variable i. As far as I can see there's no reason for you to be defining it. Hope this helps, good luck!

Selection sort a roster

So I'm supposed to create a program that asks for the class size. Then using that size, you enter a students name and score until you fill the class size. After doing that, I'm supposed to invoke a selectionSort method to sort the scores in descending order. So the output is essentially a table. One column is name, the other is score and the scores are supposed to be in descending order with their appropriate name. I have most of the program down, I just can't figure out how to tie the students name to their score that was entered. Can someone steer me in the right direct? I'm at a loss. Here is my program:
import java.util.Scanner;
public class Roster {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
input.useDelimiter(System.getProperty("line.separator"));
System.out.print("Enter the size of the class: ");
int size = input.nextInt();
double[]score = new double[size];
String[]name = new String[size];
for(int i = 0; i < size; i++){
System.out.print("Please enter a student name: ");
String n = input.next();
name[i] = n;
System.out.print("Please enter " + n + "'s score: ");
double s = input.nextDouble();
score[i] = s;
System.out.println();
}
selectionSort(score,name);
System.out.print("THe class size is: " + size + "\n");
System.out.print("Name Score\n");
System.out.print("---- -----\n");
for(int i = 0; i < name.length; i++)
System.out.println(name[i] + " " + score[i] + " ");
System.out.println();
}
public static void selectionSort(double[] score, String[] name){
for(int i = score.length-1; i > 0; i--){
int maxIndex = 0;
for(int j = 1; j <= i; j++)
if(score[j] < score[maxIndex])
maxIndex = j;
double temp = score[i];
score[i] = score[maxIndex];
score[maxIndex] = temp;
}
}
}
I already commented a simple solution, but really the best thing to do would be to create a class of roster entries:
public class RosterEntry {
private String name;
private double score;
/*Accessors and Mutators*/
}
Then in your main you can maintain a list or array of RosterEntrys so that when you make swaps in the selection sort, you swap RosterEntrys instead of scores and names individually.
You could create a class of pairs, which you store in an array. As such:
public class MyPair
{
private final String name;
private final Double score;
public MyPair(String aName, Double aScore)
{
name= aName;
score = aScore;
}
public String getName() { return name; }
public Double getScore() { return score; }
}
You can then create a simple array of type MyPair,
MyPair[] pupils = new MyPair(20);
You can access details of pupils by
pupils[i].getName;
or
pupils[i].getScore;

Categories