Array of arrays in Java
So I want to start and say I am new to Java programming. Here is my problem. I want to create a program that in essence cycles through an array of arrays. The arrays need to be variable lengths.
My first step is I start out with array of scores. Each array is connected to a specific users. These arrays will be of differing lengths depending on the user.
My second step is to group these user arrays into a larger array with the individual arguments being the user arrays from step 1. This array should be variable length, depending on the number of users.
My final step, is to run this larger array through a series of mathematical operations. I knwo how to create the calculations. This is a main method doing the calculations
import java.lang.Math;
public class Advisor_Score {
public static void main(String[] args)
{
double review_sum[]={5,5,5,5,5,5,5,5,5,5};
//Every time a user answers a question, they will be rated. This is the array holds these ratings.
//A given user will have ten advisor scores, one for each category.
double sum = 0;
{
for(int x=0;x<review_sum.length;x++)
//All scores less than 0 or more than 5 are thrown out
{
if (review_sum[x]<0 || review_sum[x]>5)
{
sum+=0;
}
else
sum+=review_sum[x];
}
double raw_advisor=(sum-(3*review_sum.length))/4;
System.out.println(raw_advisor);
double advisor_score_scaled= 2.5*(1-Math.pow(Math.E, -.5*raw_advisor));
double advisor_score = 2.5 + advisor_sco开发者_开发技巧re_scaled;
System.out.println(advisor_score);
}
}
}
Eventually I want to loop through the larger array, running each argument in the larger array through this set of computations. I am not wedded to arrays, as I have been told that list might work better. I am just unschooled as to how to use lists. Thanks in advance!
If I'm reading your question right, it looks like you want to map a user-name to a list of scores. You can do something like this (this example is contrived, and so values are hardcoded):
Map<String, List<Integer>> userScores = new LinkedHashMap<String, List<Integer>>();
String user1 = "Bob";
List scores1 = new ArrayList<Integer>();
scores1.add(5);
scores1.add(10);
scores1.add(15);
userScores.put(user1, scores1);
...
...
String user4 = "Rob";
List scores4 = new ArrayList<Integer>();
scores4.add(2);
scores4.add(3);
scores4.add(4);
userScores.put(user4, scores4);
Then you can iterate over the map like this and access the users and their scores:
for(Map.Entry<String, List<Integer>> entry : userScores.entrySet()) {
String user = entry.getKey();
List<Integer> scores = entry.getValue();
//to iterate over the list of scores
for(int score : scores) {
...
}
}
To answer your general question about an array of arrays, you can have a list of lists like this:
List<List<Integer> listOfLists = new ArrayList<List<Integer>>();
So you'd create lists and add them to your list of lists like this:
List<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(2);
list1.add(3);
listOfLists.add(list1);
...
public class Array2 {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
// Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}
精彩评论