Create values for each time the for loop loops - java

So I'm trying to create a for loops that creates a new value each times it runs.
Something like this:
for(int i = 0; i < con.length; i++) {
Vector max[i] = new Vector(i+1, i+2, i+3);
}
I'm kind of new to java, so i don't understand it very well.
Thanks for your help.

This should work
Vector[] max = new Vector[con.length];
for(int i = 0; i < con.length; i++) {
max[i] = new Vector(i+1, i+2, i+3);
}

Related

Two dimension array, how to programically add new array element? Java

I don't know, if I forgot how, or I just can't figure it out how.
For example :
Object[][] data = {
{"id", "projectname","valueid", "value"},
};
And this is how they should be added, but in loop:
Object[][] data = {
{"id", "projectname","valueid", "value"},
{"id2", "projectname2","valueid2", "value2"},
{"id3", "projectname3","valueid3", "value3"},
};
And so on..
I need a tip only, like a skeleton how it should be. I tried to figure it out, but had no idea how.
Thanks!
You can add a new array to another array like this :
data[1] = new Object[]{"id_1", "projectname_1","valueid_1", "value_1"};
...
data[n] = new Object[]{"id_n", "projectname_n","valueid_n", "value_n"};
You can use this way in any loop for example :
int length = 5;
Object[][] data = new Object[length][];
for(int i = 0; i < length; i++){
data[i] = new Object[]{...some information};
}
for (int i = 1; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
int line = i+1;
data[i][j] = data[0][j]+ line;
}
}

Java - Filling multidimensional (2d) ArrayList like a 2d array

A while ago before I got used to object object oriented programming I created a basic TicTacToe game and to create the board I used an array.
The code is a complete mess because I didn't properly understand how to use objects, but I did initialize the board correctly:
char[][] board = new char[3][3];
for (int i = 0; i < board.length; i++){
for (int j = 0; j < board[i].length; j++){
board[i][j] = '[]' //or something like that...don't remember exactly
}
}
My question is how would you this with an ArrayList?
ArrayList <ArrayList<Character>> board = new ArrayList(); // this initialization is not
// wrong using Java 8 but for earlier versions you would need to state the type on both
//sides of the equal sign not just the left
for (int i = 0; i < board.size(); i++){
for (int j = 0; j < board.get(i).size(); j++){
board.get(i).get(j).add('[]');
}
}
but that does not work.
It does not have to be exactly like this, I just generally want to understand how to handle multidimensional ArrayLists.
-thanks
Unlike arrays, you can't initialize an entire ArrayList directly. You can specify the expected size beforehand (this helps performance when you are using very large lists, so it is a good practice to do it always).
int boardSize = 3;
ArrayList<ArrayList<Character>> board = new ArrayList<ArrayList<Character>>(boardSize);
for (int i = 0; i < boardSize; i++) {
board.add(new ArrayList<Character>(boardSize));
for (int j = 0; j < boardSize; j++){
board.get(i).add('0');
}
}
The main difference is that in your original code you had a multi-dimensional array of primitives (in this case, char) and all you had to do was assign a new primitive value to each slot in the array.
However what you want now is an ArrayList of (ArrayList of Character). When you create the ArrayList it is empty. In order to procede you are going to need to fill it with several (ArrayList of Character) before you can begin to start adding Characters themselves.
So for example,
ArrayList <ArrayList<Character>> board = new ArrayList<>();
for (int i=0; i<3; i++) {
board.add(new ArrayList<Character>());
}
Now you can start adding Characters to your lists:
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
board.get(i).add('A');
}
}
Hope this helps.
First you have to initialize the ArrayList in your first line correctly and than you have to initialize an new ArrayList in each run of your first loop:
ArrayList <ArrayList<Character>> board = new ArrayList<ArrayList<Character>>();
for (int i = 0; i < 3; i++){
ArrayList<Character> innerList = new ArrayList<Character>();
board.add(innerList);
for (int j = 0; j < 3; j++){
innerList.add('[');
}
}

Java How to iterate through an array of doubles and round to nearest whole number

Tried a couple of things. I need to iterate through an array of doubles. And round each element to the nearest whole number. Any ideas where I'm going wrong ?
for(int i = 0; i < example.length; i++){
Math.round(example[i]);
}
int[] example1 = new int[example.length];
for(int i=0; i<example1.length; i++) {
Math.round(example1[i]);
example1[i] = (int) example[i];
}
for(int i = 0; i < example.length; i++){
Math.round(example[i]);
}
In the loop above, you are not assigning the value of Math.round() to a variable and hence you lose it.
If you do not need the double[]'s values, you can assign it back to the same element. So, you loop would look as follows:
for(int i = 0; i < example.length; i++){
example[i] = Math.round(example[i]); // assigning back to same element
}
else, put it into a different array, possibly an int[]. Then, it would look as follows:
int[] roundedValues = new int[example.length];
for(int i = 0; i < example.length; i++){
roundedValues[i] = (int) Math.round(example[i]); // into new array
}
you need to assign Math.round to a variable.
try this:
for(int i = 0; i < example.length; i++){
example[i] = Math.round(example[i]);
}
You can try this :
for(int i = 0; i < example.length; i++){
example[i] = Math.round(example[i]);
}
You don't need 2 loops.
You are not using the result returned from Math.round().
You are trying to cast a double to an int - no need to do that.
Try:
double[] exmaple = //get your array of doubles
long[] rounded = new long[example.length];
for (int i=0; i<example.length; i++) {
rounded[i] = Math.round(example[i]);
}

ArrayList of ArrayLists - clear function confusion

Here is a particular method I have written:
class A {
private static ArrayList<ArrayList<Integer>> inputTerms = new ArrayList<ArrayList<Integer>();
public static void method1(ArrayList<Integer> terms) {
ArrayList<Integer> clauses = new ArrayList<Integer>();
int N = terms.size();
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
clauses.clear();
}
}
}
}
This method is called multiple times from the main function.
In the end, i try to write the contents of the class variable into a file. However, when I do this, i get 0 as the contents of inputTerms. However, if i remove the clauses.clear() line, i am able to get approppriate values.
My program is such that it is vital for me to clear the clauses after adding to inputTerms. Is there any alternative to this?
**Hmmm.. I have done what you've suggested. However, I haven't quite overcome the problem. To give more background, in my main function, I have the following code:
for (int i=0; i<N-1; i++){
ArrayList<Integer> firstdiagonalTerms = new ArrayList<Integer>();
for (int j=0; j<N-i; j++){
firstdiagonalTerms.add(variable[j][i+j]);
}
method1(firstdiagonalTerms);
}
I have to call the method1 function 4 times for different combinations of 'i' and 'j'. However, I still get 0 when I use the above mentioned suggestions**
You are adding the same list and clearing it repeatedly. When you add an object to a list it copies a reference to it, not a copy of the object.
int N = terms.size();
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
List<Integer> clauses = new ArrayList<Integer>();
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
}
}
or
for (int i = 0, N = terms.size(); i < N - 1; i++)
for (int j = i + 1; j < N; j++)
inputTerms.add(Arrays.asList(-terms.get(i), -terms.get(j)));
Not sure i understand what you are trying to achieve, but you keep reusing the same list, which is probably not what you meant to do.
You should probably move the ArrayList<Integer> clauses = new ArrayList<Integer>(); inside the inner loop, and not call clauses.clear() at all.
When you are adding "clauses" you are adding the actual object to the arrayList, not a copy. So when you clear them all the values in the list will be removed. To get arround this, add a clone of the list:
inputTerms.add((ArrayList<Integer>) clauses.clone());
When you call clear() on list, you are updating/removing same objects (because list contains reference to objects, not copy of object). That is what causing the issue.
I think you need to do something like below. Instead of using clear(), create a new list everytime.
public static void method1 (ArrayList<Integer> terms)
{
int N = terms.size();
for (int i = 0; i<N-1; i++) {
for (int j=i+1; j<N; j++) {
ArrayList<Integer> clauses = new ArrayList<Integer>();
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
}
}

How to declare and assign an array without the app crashing?

Basically, I'm implemeting a reversi app for android for my year 13 coursework and this snippet of code is meant to setup the board which is shown here as an array of the class position. However, when run, the app crashes.
Position[][] board = new Position[7][7]; //declaring the board//
for(int n = 0; n < 8; n ++){
...
for(int i = 0; i < 8; i++ ){
final ImageView button = new ImageView(this);
final int countN = n;
final int countI = i;
board[countI][countN].isPositionEmpty = true; //assigning a value//
Any help would be much appreciated!! thanks in advance!
You've only allocated a 7x7 array, but you're trying to use it as an 8x8 array.
Change to use:
Position[][] board = new Position[8][8];
Or preferably, have a constant which is used in multiple places:
private static final int BOARD_SIZE = 8;
...
Position[][] board = new Position[BOARD_SIZE][BOARD_SIZESIZE];
for (int i = 0; i < BOARD_SIZE; i++)
{
...
}
An array allocation like this:
Foo[] array = new Foo[size];
creates an array with size elements; valid indexes are in the range 0 to size - 1 inclusive.
You have to insantiate every index of your matrix too.
for(int i = 0; i < 8; i++ ){
board[countI][countN] = new Position();
board[countI][countN].isPositionEmpty = true; //assigning a value//
}
// Bad:
Position[][] board = new Position[7][7];
for(int i = 0; i < 8; i++ ){
...
// Better:
Position[][] board = new Position[8][8];
for(int i = 0; i < 8; i++ ){
...
// Best:
Position[][] board = new Position[8][8];
for(int i = 0; i < board[0].length; i++ ){
...
PS:
You not only need to initialize the array (allocate space for each row and each column of your "container"); you*also* need to initialize each element of the array (e.g. "array[i][j] = new Position()").
Your for loop goes from 0 to 7 which is actually 8 cells. So you need 8 position objects.
Position[][] board = new Position[8][8]
or if you want it to be a 7 by 7 board you need to stop at the 6th index
for (int i =0; i <7 ; i++)

Categories