I've been trying to copy the columns of a 2D array into a 1D array. The Stock class I created has a double[] array named data. The last loop tries to print some values that should be in the first Stock object, but it actually prints the values from the last object.
Stock arr[] = new Stock[numcols]; // creates array with the same n of slots as columns
double[] temp = new double[numrows-1];
for(int i=1; i<numcols; i++){
for(int j=1; j<numrows; j++){
temp[j-1] = fluct[j][i];
}
arr[i-1] = new Stock(temp, compName[i-1], price[i-1]);
}
for(int i=0; i<numrows/20; i++)
System.out.println(arr[0].data[i] + arr[0].name);
In fact, if I loop printing arr[j].data[i] it will print the same values for all j. It seems the loop is creating all objects with the same values for each Stock, but I see no reason why it is doing so.
I've checked the 2D array fluct and all values there are in order. I start the loops at position 1 as the values in position 0 are of no interest. Also tried printing separately the values of temp[] and they were correct, but still the data in the objects was wrong.
Here's the Stock object (omitted the getMean/getDev methods for brevity):
public class Stock{
public static double[] data;
public static String name;
public static double stDev;
public static double price;
public static double mean;
public Stock(double[] newData, String newName, double newPrice){
this.data = newData;
this.name = newName;
this.price = newPrice;
this.mean = getMean();
this.stDev = getDev();
}
}
The problem is with were you define your temp array. You should do it inside the first for loop:
Stock arr[] = new Stock[numcols]; // creates array with the same n of slots as columns
for(int i=1; i<numcols; i++){
double[] temp = new double[numrows-1];
for(int j=1; j<numrows; j++){
temp[j-1] = fluct[j][i];
}
arr[i-1] = new Stock(temp, compName[i-1], price[i-1]);
}
for(int i=0; i<numrows-1; i++)
System.out.println(arr[0].data[i] + arr[0].name);
This way for every arr element a new temp will be used. Currently you are using the same temp object and it's values are being updated as well as the values for arr elements.
Also I've changed to condition for the last for loop to i<numrows-i. Don't know why you needed i<numrows/20 =)
Good luck with Java studying!
Well, this is embarrassing, but I finally figured out what was wrong. In my object class I had my variables static! Removing that solved it.
Related
I am pretty new in this world, and I must say sometimes things that looks easy are pretty harsh.
I am stuck with a task that entails dealing with an array and for-loops.
I should iterate over the array and for every iteration step print a different random string. My current code is not working correctly, the only thing I'm getting a random item and the same index printed multiple times.
My output right now:
relax
2
2
2
2
How can I fix that and get a correct randomized output?
My code:
public static void main(String[] args) {
int i;
String Cofee[] = {"pick it","drink it","relax","put it in a cup",};
java.util.Random randomGenerator = new java.util.Random();
int x = Cofee.length;
int y = randomGenerator.nextInt(x);
String frase = Cofee[y] ;
System.out.println(frase);
for(i = 0; i < Cofee.length; i++)
System.out.println(y);
}
You assign a value to y once, and you print y repeatedly. The value of y doesn't change. To do that, you would need to call randomGenerator.nextInt(x) for each iteration of the loop!
However, if you want to randomize and print the array, use:
public static void main(String[] args)
{
String[] coffee = {"pick it","drink it","relax","put it in a cup",};
// this wraps the array,
// so modifications to the list are also applied to the array
List<String> coffeeList = Arrays.asList(coffee);
Collections.shuffle(coffeeList);
for(String value : coffee)
System.out.println(value);
}
As an aside, don't use String coffee[], but use String[] coffee. Although Java allows putting the array type after the variable name, it is considered bad form.
Or use a list directly:
public static void main(String[] args)
{
List<String> coffeeList = Arrays.asList("pick it","drink it","relax","put it in a cup");
Collections.shuffle(coffeeList);
for(String value : coffeeList)
System.out.println(value);
}
For that, you can implement a shuffling algorithm.
It's not so scaring as it might sound at first. One of the famous classic shuffling algorithms, Fisher–Yates shuffle is relatively easy to grasp.
The core idea: iterate over the given array from 0 to the very last index, and for each index swap the element that corresponds to the randomly generated index between 0 and the current index (i) with the element under the current index.
Also, I would advise creating a separate array representing indices and shuffle it in order to preserve the array of string its initial state (you can omit this part and change the code accordingly if you don't need this).
That's how it might be implemented:
public static final Random RANDOM = new Random(); // we need an instance for random to generate indices
A Fisher–Yates shuffle implementation:
public static void shuffle(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int j = RANDOM.nextInt(i + 1); // generating index in range [0, i]
swap(arr, i, j); // swapping elements `i` and `j`
}
}
Helper-method for swapping elements:
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Usage-example:
String[] coffee = {"pick it","drink it","relax","put it in a cup"};
int[] indices = new int[coffee.length];
for (int i = 0; i < indices.length; i++) indices[i] = i; // or Arrays.setAll(indices, i -> i); if you're compfortable with lambda expressions
shuffle(indices);
for (int i = 0; i < coffee.length; i++) {
String next = coffee[indices[i]];
System.out.println(next);
}
Output:
drink it
pick it
put it in a cup
relax
Write a method sum(int[] values) that returns the sum of elements of the array values.
when I display my sum method it only displays the last element of the array
(I'm still a beginner, so go easy on me please)
System.out.print("Generated numbers for player 1 are: ");
int values1 = 0;
for (int i = 0; i <= 10; i++) {
values1 = 1+(int)(Math.random()*(6-1));
System.out.print(values1+" ");
}
System.out.print("\nGenerated numbers for player 2 are: ");
int values2 = 0;
for (int i = 0; i <= 10; i++) {
values2 = 1+(int)(Math.random()*(6-1));
System.out.print(values2+" ");
}
int[] player1 = {values1};
int[] player2 = {values2};
System.out.println("\n");
sumArray(player1);
sumArray(player2);
System.out.println(sumArray(player1));
}
public static int sumArray( int[] sum) {
int add=0;
for(int i=0; i< sum.length; i++) {
add += sum[i];
}
return add;
Main problem lies within your for loop. You are just updating values1 and values2 variable .You should be also adding them to the the arrays.
code will be
int[] player1 = new int[11];
for (int i = 0; i <= 10; i++) {
values1 = 1+(int)(Math.random()*(6-1));
player1[i] = values1;
System.out.print(values1+" ");
}
In addition you don't need these 2 lines.
int[] player1 = {values};
int[] player2 = {values2};
You are not understanding how to populate arrays
int[] player1 = new int[10];
for (int i = 0; i < 10; i++) {
values2 = 1+(int)(Math.random()*(6-1));
System.out.print(values2+" ");
player1[i] = values2;
}
I have changed the size of the array to be 10 and not as you have coded it (which result in entry of 11 elements)
Your player1 & player2 are only containing single element & that is reason you are getting sum as last element's (or single element's ) value.
If you want to have values1 containing multiple values then consider defining it as array instead of single integer.
int values1 = new int[11];
for (int i = 0; i <= 10; i++) {
values1[i] = 1+(int)(Math.random()*(6-1));
System.out.print(values1[i]+" ");
}
Similar case for your values2. After that, you can directly call:
sumArray(values1);
No need to define separate player1 & player2.
I would suggest a few things.
First, you should try not to "duplicate" the code. Here Player1 and Player2 could be just passed as parameters for a method that would generate their arrays.
Second, if you want to do any manipulations on arrays, you should avoid [] but instead use ArrayList for example.
Also, you may want to look into Object Oriented logic, as you don't seem to use class for Person, depending on what you want to achieve, you could use a class "Person" with attribute "values" which would be of type ArrayList.
As for your code, you reallocate "value1" and "value2", so they are just integers, not lists.
This would give something like this:
public static int generateValue(PersonClass person) {
ArrayList<Integer> values = new ArrayList<>();
for (int i = 0; i <= 10; i++) {
values.add(1+(int)(Math.random()*(6-1)));
}
person.setValues(values);
return sumArray(values);
}
public static int sumArray(ArrayList<Integer> values) {
int add=0;
for (Integer value : values) {
add += value;
}
return add;
}
This way, you will be able to retrieve your values and to print them as you want. The "PersonClass" part is not mandatory, it just depends if you want to keep your values associated to a Person or not (how long will you need those data).
(Example of class PersonClass :
public class PersonClass {
private ArrayList<Integer> values = new ArrayList<>();
public ArrayList<Integer> getValues() {
return this.values;
}
public void setValues(ArrayList<Integer> newValues) {
this.values = newValues;
}
}
as you should always use getters and setters). Good luck on you way to Java learning.
I'm new to Java, and I'm not sure how to ask the right question, so please bear with me. I have 40 total items of 6 different types to put into a new array; each item type has a different cost. The first item (quantity=1) costs $3, the second item (qty=2) costs $5 each, the third item (qty=4) costs $9 each, and so on. The quantity of each item type is in numTypeIndArray and the cost for each type is in costCSDriverArray. A cumulative count of the total items is in numTypeCumulArray.
So, the new array, indItemCostArray, should be single dimensional and have 40 elements. It would look something like {3,5,5,9,9,9,9,...,13,13,13}, but the last fifteen elements are a cost of $13. How do I get to this array with 40 elements? I started with trying to fill the array using a nested for loop but I haven't gotten there yet. The code below is plain wrong.
int[] costArray = new int[]{3,5,9,10,11,13};
int[] numTypeIndArray = new int[]{1,2,4,7,11,15};
int[] numTypeCumulArray = new int[]{1,3,7,14,25,40};
int[] indItemCostArray = new int[numTypeCumulArray[6]];
for (int i = 0; i < indItemCostArray.length; i++) {
for (int j = 0; j < numTypeIndArray[i]; j++) {
indItemCostArray[i+j] = costArray[j];
}
}
First of all, you'll get a ArrayOutOfBoundException at:
int[] indItemCostArray = new int[numTypeCumulArray[6]];
The size of the array numTypeCumulArray is 6, and arrays are 0 indexed. So, The last index number is 5, not 6, as indexing started from 0.
You can do as follows for accessing the last element of the array:
int[] indItemCostArray = new int[numTypeCumulArray[numTypeCumulArray.length - 1]];
Secondly, you're running your outer loop for 40 times and for each iteration your inner loop is trying to iterate for numTypeIndArray[i] times, where i is the iterator variable of outer loop. So, surely after sixth iteration, when value of i will be 6, your program will again throw the ArrayOutOfBoundException as you're accessing a value in the terminator condition of the inner loop from numTypeIndArray whose last index is 5.
Again, inside the inner loop, you're assigning indItemCostArray at index position i+j, which will actually far from your purpose.
To achieve what you are exactly expecting, you can do as follows:
int currentIndex =0;
for (int costIndex = 0; costIndex < costArray.length; costIndex++) {
for(int index = currentIndex; index < currentIndex + numTypeIndArray[costIndex]; index++) {
indItemCostArray[index] = costArray[costIndex];
}
currentIndex = numTypeCumulArray[costIndex];
}
Here, what I did is, in the outer loop I iterated the same amount of time the length of costArray, you can take the length of numTypeIndArray instead too, no issue. I've defined a variable named currentIndex to keep track of the current assignable index for array indItemCostArray. In the inner loop, I tried to begin with the currentIndex and loop upto the time same as the number of items needed for that type, given in numTypeIndArray[costIndex], and for each iteration, set the corresponding index of indItemCostArray with the cost of costIndex in the costArray. Finally, I update the currentIndex with the corresponding cumulative total items from numTypeCumulArray.
Hope you got everything clear.
The whole setup of three arrays is kind of weird. The weiredest is the third array. Think carefully, do you actually need it? You already have all the information in your second array. The third array can introduce a lot of unnacessary mistakes.
But, assuming that you actually need these arrays for some reason and there are no mistakes in making these arrays. You can get your required fourth array as follows,
int[] costArray = new int[]{3,5,9,10,11,13};
int[] numTypeIndArray = new int[]{1,2,4,7,11,15};
int[] numTypeCumulArray = new int[]{1,3,7,14,25,40};
// you want to make sure that your arrays are of same lenght
assert(costArray.length == numTypeIndArray.length && costArray.length == numTypeCumulArray.length);
// length of these arrays is unique items count
int uniqueItemsCount = costArray.length;
// totalItemsCount is last element of numTypeCumulArray
int totalItemsCount = numTypeCumulArray[uniqueItemsCount - 1];
int[] indItemCostArray = new int[totalItemsCount];
// use this to keep track of index in indItemCostArray
int itemCostIndex = 0;
for (int i = 0; i < uniqueItemsCount && itemCostIndex < totalItemsCount; i++) {
for (int j = 0; j < numTypeIndArray[i] && itemCostIndex < totalItemsCount; j++) {
indItemCostArray[itemCostIndex] = costArray[j];
// increase the index for next item cost
itemCostIndex += 1;
}
}
int[] costArray = new int[]{3,5,9,10,11,13};
int[] numTypeIndArray = new int[]{1,2,4,7,11,15};
int[] numTypeCumulArray = new int[]{1,3,7,14,25,40};
int[] indItemCostArray = new int[numTypeCumulArray[5]];
int num = 0;
for (int i = 0; i < numTypeIndArray.length; i++) {
for (int j = 0; j < numTypeIndArray[i]; j++) {
indItemCostArray[num + j] = costArray[i];
}
num += numTypeIndArray[i];
}
System.out.println(Arrays.toString(indItemCostArray));
First, you don't need int[] numTypeCumulArray = new int[]{1,3,7,14,25,40};
It just shows the cumulative values of the numTypeIndArray. The last value, 40 is just the sum of numTypeIndArray and that would be the size of the resulting array from your requirement.
It can be summed in a simple for loop or you can do it like this and then create the target array.
int maxSize = Arrays.stream(numTypeIndArray).sum();
int[] indItemCostArray = new int[maxSize];
Then you could proceed to populate the array with the values as has been shown. Here is another way using streams which you will undoubtedly learn about. The quick explanation is that it creates multiple streams of the proper quantities of cost.
e.g
stream1 -> {3}
stream2 -> {5,5};
stream3 -> {9,9,9,9} etc.
Then it flattens them in a single stream of those values and returns an array.
int[] result = IntStream.range(0, costArray.length)
.flatMap(i -> IntStream.range(0, numTypeIndArray[i])
.map(q -> costArray[i]))
.toArray();
But using a class to hold the information would be better. Here is one example.
class Product {
private String name;
private int cost;
private int quantity;
public Product(String name, int cost, int quantity) {
this.name = name;
this.cost = cost;
this.quantity = quantity;
}
public int getCost() {
return cost;
}
public int getQuantity() {
return quantity;
}
public String getName() {
return name;
}
#Override
public String toString() {
return new StringJoiner(", ","[", "]").add(name).add("cost="+cost).add("quantity="+quantity).toString();
}
}
And it can be used like so.
List<Product> products = new ArrayList<>();
for (int i = 0; i < costArray.length; i++) {
products.add(new Product("Item" + (i+1), costArray[i], numTypeIndArray[i]));
}
products.forEach(System.out::println);
Prints
[Item1, cost=3, quantity=1]
[Item2, cost=5, quantity=2]
[Item3, cost=9, quantity=4]
[Item4, cost=10, quantity=7]
[Item5, cost=11, quantity=11]
[Item6, cost=13, quantity=15]
And once again it can be streamed to create your results exactly as before only using the class getters to get the values.
int[] result2 = products.stream()
.flatMapToInt(
prod -> IntStream.range(0, prod.getQuantity())
.map(q -> prod.getCost()))
.toArray();
The two arrays result and result2 are identical. But you may find that using classes may eliminate the requirement for creating such an array.
I am trying to read in a string from a file, extract individual characters and use those characters to fill a 2D char array. So far I have been able to do everything except fill the array. I keep getting an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: error message. Any help would be appreciated. This is my first time working with 2D arrays. Thanks.
Here are the contents of the test.txt. Each word on a new line. The first 2 integers are the dimensions of the array
4 4
FILE
WITH
SOME
INFO
public class acsiiArt
{
public static void main(String[] args) throws IOException
{
File file = new File("test.txt");
Scanner inputFile = new Scanner(file);
int x = inputFile.nextInt();
int y = inputFile.nextInt();
while (inputFile.hasNext())
{
char [][] array = new char [x][y];
//char c = words.charAt(i);
for (int row =0; row<x;row++)
{
for (int col =0; col<y;col++)
{
String words = inputFile.nextLine();
for (int i=0; i<words.length(); i++)
array[x][y]=words.charAt(i);
}
}
}
}
}
for (int row =0; row<x;row++)
{
for (int col =0; col<y;col++)
{
String words = inputFile.nextLine();
for (int i=0; i<words.length(); i++)
array[x][y]=words.charAt(i);
}
}
The total number of indices in array is x * y. Below, you are filling all the possible indices
for (int row =0; row<x;row++)
{
for (int col =0; col<y;col++)
So when you add this:
for (int i=0; i<words.length(); i++)
you multiplying another factor words.length. So you need x * y * words.length number of indices, but you only have x * y. Thats why you're getting ArrayIndexOutOfBoudsException
I've seen problems like this and I'm assuming that x and y are being initialized to the first two characters which represent the number of rows and the number of columns. If that is the case, then third for loop for (int i=0; i<words.length(); i++) is unnecessary. You can just reffer to the col variable for the word at that point, since it should represent how many characters there are.
All of this is only applicable if the chars are in a rectangular pattern, meaning that there are the same number of columns in every row. Otherwise you will get an IndexOutOfBoundsError as soon as one of the lines is shorter than the the column value initially given.
Edit: If you're final 2d char array is not meant to be rectangular and instead "jagged," a different implementation is required. I'd recommend either a 2d arrayList (an arrayList of arrayLists).
Or you can keep your current implementation with the third for loop, but you have to be sure that the original x value represents the longest row/most amount of columns, and then you'd be able to deal with each row indivually with words.length. You'd also have to be fine with the extra portions of the lines that have a length>x having spaces initialized to null.
ArrayIndexOutOfBoundsException means you are using array beyond its limit. So in your case:
char [][] array = new char [x][y];
//char c = words.charAt(i);
for (int row =0; row<x;row++) {
for (int col =0; col<y;col++){
String words = inputFile.nextLine();
for (int i=0; i<words.length(); i++)
array[x][y]=words.charAt(i);
}
}
Problem may be because your array size is less then input words. problem is because you are putting extra loop, and your loop it self is not correct. Please seen code below.
So you can do 2 things.
change value of y large enough so that any word string can store.
rather than looping on size of word you can loop on your array size like.
.
for (int row =0; row<x;row++) {
String words = inputFile.nextLine();
int size = Math.min(words.length(),y);
for (int i=0; i< size; i++)
array[row][i]=words.charAt(i);
}
The easiest way to do this is with an ArrayList<char[]>. All you have to do is add a new char[] for each new line read:
ArrayList<char[]> chars = new ArrayList<>();
while (inputFile.hasNext()){
chars.add(inputFile.nextLine().toCharArray());
}
char[][] array = chars.toArray(new char[chars.size()][]);
An ArrayList is basically an array of changeable size. This code takes each line in the file, turns it into a char[], then adds it to the ArrayList. At the end, it converts the ArrayList<char[]> into a char[][].
If you can't or don't want to use ArrayList, you could always do this:
char[][] array = new char[1][];
int a = 0;
while(inputFile.hasNext()){
//read line and convert to char[]; store it.
array[a] = inputFile.nextLine().toCharArray();
//if there are more lines, increment the size of the array.
if (inputFile.hasNext()){
//create a clone array of the same length.
char[][] clone = new char[array.length][];
//copy elements from the array to the clone. Note that this can be
//done by index with a for loop
System.arraycopy(array, 0, clone, 0, array.length);
//make array a new array with an extra char[]
array = new char[array.length + 1][];
//copy elements back.
System.arraycopy(clone, 0, array, 0, clone.length);
a++;
}
}
If you know the dimensions of the array beforehand:
char[][] array = new char[dimension_1][];
int a = 0;
while (inputFile.hasNext()){
array[a] = inputFile.nextLine().toCharArray();
a++; //don't need to check if we need to add a new char[]
}
In response to comment:
We know that a char[][] cannot be printed with Arrays.toString() (if we want the contents) because we will get a lot of char[].toString(). However, a char[][] can be printed with one of the following methods:
public static String toString(char[][] array){
String toReturn = "[\n";
for (char[] cArray: array){
for (char c: cArray){
toReturn += c + ",";
}
toReturn += "\n";
}
return toReturn + "]";
}
I personally prefer this one (requires import java.util.Arrays):
public static String toString(char[][] array){
String toReturn = "[\n";
for (char[] cArray: array){
toReturn += Arrays.toString(cArray) + "\n";
}
return toReturn + "]";
}
I'm revising for a introduction to programming exam and I have a question which from a previous exam paper I am slightly stuck on.
The question:
Write a method that takes a double array as an argument with values representing the positions of train stations along a track. The method should return a two-dimensional array with the distances between each pair of stations in the argument. The array of distances should have only one entry for each pair of stations (i.e. do not use a rectangular array).
I have a solution to the question but I just can't get the last bit where there should only be one entry for each pair. I have thought about creating a look up table will all the entries to see if the distance for the two stations but then the array would have lots of empty cells for the later stations because the distance would have already been calculated.
Here is my current solution
//Set of locations on the train line
private static double[] stations = {0.0, 2.0, 3.0, 5.0};
//Method to take the array of doubles and create distance map
public static double[][] getDistances(double[] st){
double[][] distanceMap = new double[st.length][st.length-1];
int x;
for(int i=0; i<st.length; i++){
x=0;
for(int j=0; j<st.length; j++){
if(j != i){
distanceMap[i][x] = Math.abs(st[i]-st[j]);
x++;
}
}
}
return distanceMap;
}
//Main method to get the distance map then loop over results
public static void main(String[] args){
double[][] arrayMatrix = getDistances(stations);
for(int i=0; i<arrayMatrix.length; i++){
for(int j=0; j<arrayMatrix[0].length; j++){
System.out.print(arrayMatrix[i][j]+" ");
}
System.out.println("");
}
}
If anyone could point me in the right direction it would be really appreciated.
Thanks in advance.
//EDIT
After some great advice from #izomorphius I have managed to solve the question. Thanks.
Here is the complete solution
//Set of locations on the train line
private static double[] stations = {0.0, 2.0, 3.0, 5.0};
//Method to take the array of doubles and create distance map
public static double[][] getDistances(double[] st){
double[][] distanceMap = new double[st.length-1][];
int size = st.length-1;
for(int i=0; i<distanceMap.length; i++){
distanceMap[i] = new double[size];
size--;
}
ArrayList<String> lut = new ArrayList<String>();
int x;
for(int i=0; i<distanceMap.length; i++){
x=0;
for(int j=0; j<st.length; j++){
if(j != i && !lut.contains(i+"/"+j)){
distanceMap[i][x] = Math.abs(st[i]-st[j]);
lut.add(i+"/"+j);
lut.add(j+"/"+i);
x++;
}
}
}
return distanceMap;
}
//Main method to get the distance map then loop over results
public static void main(String[] args){
double[][] arrayMatrix = getDistances(stations);
for(int i=0; i<arrayMatrix.length; i++){
for(int j=0; j<arrayMatrix[i].length; j++){
System.out.print(arrayMatrix[i][j]+" ");
}
System.out.println("");
}
}
What the statement says is "i.e. don't use rectangular array". The idea is to store only one value for each pair. For instance if you have the pair (a,b) and a < b store the distance between a and b in the array of a but not in the one of b. Thus the array for the first station will be of size n - 1(distances to all the other stations), for the second station it will be of size n - 2(all other stations except the first one) and so on. Therefor your array will be triangular not rectangular. I hope this tip is enough as after all the idea is not to have me solve your problem.