Iteration sum in a linkedlist in java - java

I have 3 linked lists, in java with this values:
LinkedList<Double> simTarget = new LinkedList<Double>();
LinkedList<Double> simSource = new LinkedList<Double>();
LinkedList<Double> results = new LinkedList<Double>();
simTarget.add(0.5);
simTarget.add(0.1);
simTarget.add(1.0);
simSource.add(0.5);
simSource.add(0.1);
simSource.add(1.0);
I need to perform an iteractive sum in these lists and the results I stored in the "results", for example:
for (int i = 0; i < simTarget.size(); i++)
{
for (int j = 0; j < simSource.size(); j++)
{
results.add((simTarget.get(i) + (simSource.get(j) * 0.5)/3));
}
}
So, this is my problem: with the linkedlist "results" is that I need to perform again a new iteration but with the same values of the simSource, like this:
for (int i = 0; i < results.size(); i++)
{
for (int j = 0; j < simSource.size(); j++)
{
System.out.println(results.get(i) + (simSource.get(j) * 0.5)/3);
}
}
this iteration need performing (itself) in "x" times, for example: 70 times or 80 times or 1000 times. And just the values of the "results" linkedlist shall increase.
How to perform and to develop this iteration by x times?
Thank you very much.

Because Double is both final and immutable, you will have to encapsulate Double in a custom class, 'Value' for example. Then, you will update the Double field in the Value class each time you do a summation. Therefore, only the object of type Value strored in results will be updated.
for(int x = 0;x< 1000;x++){
for (int i = 0; i < results.size(); i++)
{
for (int j = 0; j < simSource.size(); j++)
{
Value buffer = results.get(i);
buffer.doubleField += (simSource.get(j) * 0.5)/3;
//System.out.println(results.get(i) + (simSource.get(j) * 0.5)/3);
}
}
}
BTW, since you are accessing the LinkedList by index, it will be better to use an ArrayList instead.

Related

How to check how long it takes to sort out random integers using bubblesort in java?

I want to see how long it takes for 10,000 random integers to be sorted. Since in a bubblesort, the arrays are sorted at each stage and it could also vary each time, I want to know the total time it takes for the final sorting array to appear. So my time calculations should be when each sorting of the array is taking place, and when the final sorting happens and the results appear, the output should tell me the time in seconds.
I have used System.currentTimeMillis(); for this task but how would I use it so it calculates the time at each sorting stage? I have used it inside the for (int k = 0; k < numbers.length; k++){ loop because this loops through all the stages of the sorting, but my program would not output anything. How would I fix that?
Code:
class Main {
public static void main(String[] args) {
// Clear screen
System.out.print("\033[H\033[2J");
System.out.flush();
double msStartTime = 0d;
double msEndTime = 0d;
// Initialize an int array variable and set the limit to 10,000
int numbers[] = new int[10000];
// Generate random 10,000 integers to bubblesort
for (int x = 0; x < numbers.length; x++) {
numbers[x] = (int) (Math.random() * 10001);
}
for (int i = 0; i < numbers.length; i++) {
for (int j = i; j < numbers.length; j++) {
if (numbers[j] < numbers[i]) {
int temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
}
for (int k = 0; k < numbers.length; k++){
msStartTime = (double) System.currentTimeMillis();
}
}
msEndTime = (double) System.currentTimeMillis();
System.out
.println("To sort an array of 10,000 integers, it takes " + (msEndTime - msStartTime) / 1000 + " seconds");
}
}
i think you can use StopWatch.here is how u can add it to maven and use it
https://www.baeldung.com/java-measure-elapsed-time

How do I print nested ArrayLists?

My fourth for loop, for (int y), keeps printing the first m elements over and over again, how can i fix it so that it prints m elements at a time but not the same ones?
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int m = input.nextInt();
ArrayList<String> myname = new ArrayList<String>(n);
ArrayList<Integer> myscore = new ArrayList<Integer>(m);
for (int i = 0; i < n; i++) { //swimmers
myname.add(input.next());
for (int j = 0; j < m; j++) { //judges
myscore.add(input.nextInt());
}
}
for (int x = 0; x < n; x++) { //name
System.out.println(myname.get(x));
for (int y = 0; y < m; y++) { //score
System.out.println(myscore.get(y));
}
}
Based on your code it seems that you have ‘n’ number of swimmers, each with ‘m’ number of scores. You are storing the names of the ‘n’ swimmers in an ArrayList, which is bad because you know the number will never change. A better approach to this would be to declare myname as a String[] of size n, and instead of calling myname.get(x) you would later call myname[x].
This however, is only symptomatically related to the problem at hand. You are storing all of your score results inside a single ArrayList. A better solution is to generate ‘n’ number of arrays (which is what I assume you would like to do based on the title of this question). This can be done by simply declaring
allScores[][] = new int[n][m]
This would let you access the values for swimmer number ‘n’ with allScores[n]. If this isn’t what you actually wanted to do then you can simply offset the values in your last get statement by the number of scores you’ve already processed (x*n).
TLDR: Change the line in your last for loop to read:
System.out.println(myscore.get(y + x*n)
Because you have a list myscore of n*m length not only m like you thought. You are adding at the end of the list every score.
So you have n blocks of m elements in the list. You could still print the value with
for(int y = x * m, to = x*m + m; y < to; ++y){
System.out.println(myscore.get(y));
}
class ScoreHolder{
String name = "";
ArrayList<Integer> scores = new ArrayList<Integer>;
public ScoreHolder(String name){
this.name = name
}
}
And then
ScoreHolder[] scores = new ScoreHolder[n];
for (int i = 0; i < n; i++) { //swimmers
scores[i] = new ScoreHolder(input.next());
for (int j = 0; j < m; j++) { //judges
scores[i].scores.add(input.nextInt());
}
}
for (int x = 0; x < n; x++) { //name
System.out.println(scores[x].name);
for (int y = 0; y < m; y++) { //score
System.out.println(scores[x].scores.get(y));
}
}
It won't be just easy to work with now but also a lot easier to make any changes or do anything else you want.
What I have done is simply created a holder class which will hold the swimmer's name and a list of all his scores.
This abstraction will now help you in getting the scores of the swimmers or doing anything else you now want with it.

I need to make permutations with an array using the ArrayList Class. I can't figure out what's wrong

so im taking an ap comp sci class in school, and in the class we're learning about the basics of java. For this assignment we have to make permutations by taking numbers from one one-dimensional array, and putting in another, then deleting that number so it can't be picked again. The numbers in the array can't repeat. We have to use the ArrayList Class too. And I can't figure out what's wrong!
This is the method that creates the permutations:
public static ArrayList<Integer> createPerm()
{
ArrayList<Integer> list = new ArrayList<Integer>(10);
Integer x = 1, remove = 0;
for (Integer i = 0; i < 10; i++)
{
list.add(x);
x++;
}
ArrayList<Integer> perm = new ArrayList<Integer>(10);
for(Integer i = 0; i < 10; i++)
{
Integer r = (int)(Math.random() * 10) + 1;
for (Integer j = 0; j <= list.size() - 1; j++)
{
if (list.get(j) == r)
{
remove = j + 1;
list.remove(remove);
perm.add(r);
}
}
}
return perm;
}
I think that you (and I also:)) got a lttle bit confused because you are using Integer-objects as index and as list elements.
That is no problem with the List.get method, because there is only one get method which is expecting an int and Java converts the Integer to int.
The problem is in your usage of list.remove(). There are two methods, one expects an object and one an int.
So if you pass an Integer object, the remove(Object) method is called. But you pass the index, not the r-matching object, so the remove method fails sometimes, because it is random if the element is in your list if remove was called before. And if the method not fails, you have removed the element with the value of your index(+1), not the one who matches r.
for(Integer i = 0; i < 10; i++)
{
Integer r = (int)(Math.random() * 10) + 1;
for (Integer j = 0; j <= list.size() - 1; j++)
{
if (list.get(j) == r)
{
remove = j + 1;
list.remove(remove);//the main error is here you found 4
//on index 2 and removes 3 (because of +1)
perm.add(r);
}
}
}
The next thing is, that random can deliver the same number more than once,
so you should not loop only 10 times. Loop until the list is empty.
I have corrected the code as below, the original lines are commented before the correction.
//for (Integer i = 0; i < 10; i++) {
while (!list.isEmpty()) {
Integer r = (int) (Math.random() * 10) + 1;
for (Integer j = 0; j <= list.size() - 1; j++) {
//if (list.get(j) == r) {
if (list.get(j).equals(r)) {
//remove = j + 1;
remove = list.get(j);
list.remove(remove);
perm.add(r);
}
}
}
And here I put the code somewhat more clearly, so that it is easier to read
public static ArrayList<Integer> createPerm() {
ArrayList<Integer> list = new ArrayList<Integer>(10);
for (int i = 0; i < 10; i++) {
list.add(i+1);
}
ArrayList<Integer> perm = new ArrayList<Integer>(10);
while (!list.isEmpty()) {
int r = (int) (Math.random() * 10) + 1;
for (int j = 0; j < list.size(); j++) {
if (list.get(j).intValue() == r) {
perm.add(list.remove(j));
}
}
}
return perm;
}

How to create multiple arrays with a loop?

I am having trouble creating multiple arrays with a loop in Java. What I am trying to do is create a set of arrays, so that each following array has 3 more numbers in it, and all numbers are consecutive. Just to clarify, what I need to get is a set of, let's say 30 arrays, so that it looks like this:
[1,2,3]
[4,5,6,7,8,9]
[10,11,12,13,14,15,16,17,18]
[19,20,21,22,23,24,25,26,27,28,29,30]
....
And so on. Any help much appreciated!
Do you need something like this?
int size = 3;
int values = 1;
for (int i = 0; i < size; i = i + 3) {
int[] arr = new int[size];
for (int j = 0; j < size; j++) {
arr[j] = values;
values++;
}
size += 3;
int count = 0;
for (int j : arr) { // for display
++count;
System.out.print(j);
if (count != arr.length) {
System.out.print(" , ");
}
}
System.out.println();
if (i > 6) { // to put an end to endless creation of arrays
break;
}
}
To do this, you need to keep track of three things: (1) how many arrays you've already created (so you can stop at 30); (2) what length of array you're on (so you can create the next array with the right length); and (3) what integer-value you're up to (so you can populate the next array with the right values).
Here's one way:
private Set<int[]> createArrays() {
final Set<int[]> arrays = new HashSet<int[]>();
int arrayLength = 3;
int value = 1;
for (int arrayNum = 0; arrayNum < 30; ++arrayNum) {
final int[] array = new int[arrayLength];
for (int j = 0; j < array.length; ++j) {
array[j] = value;
++value;
}
arrays.add(array);
arrayLength += 3;
}
return arrays;
}
I don't think that you can "create" arrays in java, but you can create an array of arrays, so the output will look something like this:
[[1,2,3],[4,5,6,7,8,9],[10,11,12,13...]...]
you can do this very succinctly by using two for-loops
Quick Answer
==================
int arrays[][] = new int[30][];
for (int j = 0; j < 30; j++){
for (int i = 0; i < (j++)*3; i++){
arrays[j][i] = (i++)+j*3;
}
}
the first for-loop tells us, via the variable j, which array we are currently adding items to. The second for-loop tells us which item we are adding, and adds the correct item to that position.
All you have to remember is that j++ means j + 1.
Now, the super long-winded explanation:
I've used some simple (well, I say simple, but...) maths to generate the correct item each time:
[1,2,3]
here, j is 0, and we see that the first item is one. At the first item, i is also equal to 0, so we can say that, here, each item is equal to i + 1, or i++.
However, in the next array,
[4,5,6,7,8,9]
each item is not equal to i++, because i has been reset to 0. However, j=1, so we can use this to our advantage to generate the correct elements this time: each item is equal to (i++)+j*3.
Does this rule hold up?
Well, we can look at the next one, where j is 2:
[10,11,12,13,14...]
i = 0, j = 2 and 10 = (0+1)+2*3, so it still follows our rule.
That's how I was able to generate each element correctly.
tl;dr
int arrays[][] = new int[30][];
for (int j = 0; j < 30; j++){
for (int i = 0; i < (j++)*3; i++){
arrays[j][i] = (i++)+j*3;
}
}
It works.
You have to use a double for loop. First loop will iterate for your arrays, second for their contents.
Sor the first for has to iterate from 0 to 30. The second one is a little less easy to write. You have to remember where you last stop and how many items you had in the last one. At the end, it will look like that:
int base = 1;
int size = 3;
int arrays[][] = new int[30][];
for(int i = 0; i < 30; i++) {
arrays[i] = new int[size];
for(int j = 0; j < size; j++) {
arrays[i][j] = base;
base++;
}
size += 3;
}

MultiDimensional ArrayList in kmeans clustering algorithm

I am trying to implement kmeans algorithm for a certain Music Recommendation System in Java.
I have generated 2 arrays,playsFinal[](the total play-count of an artist by all users in the dataset) and artFinal[] (the unique artists in the entire dataset) . The playcount of every artFinal[i] is playsFinal[i]. For k,I have chosen kclusters=Math.sqrt(playsFinal.length)/2.
I have an array clusters[kclusters][playsFinal.length] and the first position clusters[i][0] for every 0<i<kclusters is filled with a certain value,which is basically the initial mean as in kmeans algorithm.
int j = 0;
for (int i = 0; i < n && j < kclusters; i += kclusters) {
clusters[j][0] = weighty[j];//initial means
System.out.println(clusters[j][0]);
j++;
}
Here,weight[] is a certain score given to every artist.
Now,in the following function I am returning the index,ie,which cluster the plays[i] should be added to.
public static int smallestdistance(double a, double[][] clusters) {
a = (double) a;
double smallest = 0;
double d[] = new double[kclusters];
for (int i = 0; i < kclusters; i++) {
d[i] = a - clusters[i][0];
}
int index = -1;
double d1 = Double.POSITIVE_INFINITY;
for (int i = 0; i < d.length; i++)
if (d[i] < d1) {
d1 = d[i];
index = i;
}
return index;
}
If not obvious,I am finding the minimum distance between playsFinal[i] and the initial element in every clusters[j][0] and the one that is the smallest,I am returning its index (kfound). Now at the index of the clusters[kfound][] I want to add the playsFinal[i] but here is where I am stuck. I can't use .add() function like in ArrayList. And I guess using an ArrayList would be way better. I have gone through most of the articles on ArrayList but found nothing that could help me. How can I implement this using a multidimensional ArrayList?
Thanks in advance.
My code is put together as follows:
int j = 0;
for (int i = 0; i < n && j < kclusters; i += kclusters) {
clusters[j][0] = weighty[j];//initial means
System.out.println(clusters[j][0]);
j++;
}
double[] weighty = new double[artFinal.length];
for (int i = 0; i < artFinal.length; i++) {
weighty[i] = (playsFinal[i] * 10000 / playsFinal.length);
}
n = playsFinal.length;
kclusters = (int) (Math.sqrt(n) / 2);
double[][] clusters = new double[kclusters][playsFinal.length];
int j = 0;
for (int i = 0; i < n && j < kclusters; i += kclusters) {
clusters[j][0] = weighty[j];//initial means
System.out.println(clusters[j][0]);
j++;
}
int kfound;
for (int i = 0; i < playsFinal.length; i++) {
kfound = smallestdistance(playsFinal[i], clusters);
//HERE IS WHERE I AM STUCK. I want to add playsFinal[i] to the corresponding clusters[kfound][]
}
}
public static int smallestdistance(double a, double[][] clusters) {
a = (double) a;
double smallest = 0;
double d[] = new double[kclusters];
for (int i = 0; i < kclusters; i++) {
d[i] = a - clusters[i][0];
}
int index = -1;
double d1 = Double.POSITIVE_INFINITY;
for (int i = 0; i < d.length; i++)
if (d[i] < d1) {
d1 = d[i];
index = i;
}
return index;
}
Java's "multidimensional arrays" are really just arrays whose elements are themselves (references to) arrays. The ArrayList equivalent is to create a list containing other lists:
List<List<Foo>> l = new ArrayList<>(); //create outer ArrayList
for (int i = 0; i < 10; i++) //create 10 inner ArrayLists
l.add(new ArrayList<Foo>());
l.get(5).add(foo1); //add an element to the sixth inner list
l.get(5).set(0, foo2); //set that element to a different value
Unlike arrays, the lists are created empty (as any list), rather than with some specified number of slots; if you want to treat them as drop-in replacements for multidimensional arrays, you have to fill them in manually. This implies your inner lists can have different lengths. (You can actually get "ragged" multidimensional arrays by only specifying the outer dimension (int[][] x = new int[10][];), then manually initializing the slots (for (int i = 0; i < x.length; ++i) x[i] = new int[i]; for a "triangular" array), but the special syntax for multidimensional array creation strongly predisposes most programmers to thinking in terms of "rectangular" arrays only.)

Categories

Resources