We want to setText that will display all data from the loop but we aren't able to do so as the setText is only able to save the last int printed by the loop.
We want all the data from the loop that I have provided to be displayed.
Thanks in advance!
for(int i=0; i < intArray.length; i++){
System.out.print(intArray[i]);
}
Strictly speaking you could use the set text like so:
for(int i=0; i < intArray.length; i++) {
component.setText(comonent.getText() + intArray[i]);
}
However, this is inefficient. As #Fran Montero said, use a string builder:
StringBuilder sb = new StringBuilder();
for(int i=0; i < intArray.length; i++) {
sb.append(intArray[i]);
}
component.setText(sb.toString());
Related
I have a List called listTeams which comprises of Strings. I need to generate all unique combinations of these strings and store them in another ArrayList called lines. I've tried the following but the results are not desirable:
for(int i=0; i<listTeams.size();i++){
for(int j=1;j<listTeams.size();j++){
if (listTeams.get(j).equals(listTeams.get(i)))
continue;
for(int k=2;k<listTeams.size();k++){
if (listTeams.get(k).equals(listTeams.get(i)) || listTeams.get(k).equals(listTeams.get(j)))
continue;
String str = listTeams.get(i)+listTeams.get(j)+listTeams.get(k);
lines.put(str,new ArrayList<String>());
}
}
}
Here's the original list : {"A","B","C","D"}
What I am getting is
a_b_c
a_b_d
a_c_d
a_d_c
b_c_d
b_d_c
c_b_d
d_b_c
What I desire is:
a_b_c
a_b_d
a_c_d
b_c_d
for(int i=0; i<listTeams.size();i++){
for(int j=i+1;j<listTeams.size();j++){
for(int k=j+1;k<listTeams.size();k++){
String str = listTeams.get(i)+listTeams.get(j)+listTeams.get(k);
lines.put(str,new ArrayList<String>());
}
}
}
You need to modify your for loops like this:
for (int j = i;
and
for (int k = j;
So that only unique combinations appear
As #Berger said, the following code is working as you expect.
for (int i = 0; i < listTeams.size(); i++) {
for (int j = i+1; j < listTeams.size(); j++) {
if (listTeams.get(j).equals(listTeams.get(i)))
continue;
for (int k = j+1; k < listTeams.size(); k++) {
if (listTeams.get(k).equals(listTeams.get(i)) || listTeams.get(k).equals(listTeams.get(j)))
continue;
String str = listTeams.get(i) + listTeams.get(j) + listTeams.get(k);
lines.add(str);
}
}
}
I am working on an assignment for my java class and part of the assignment requires reading in a .csv file that is 20x20 and inserting each string into an array.
I am trying to convert my 1d array from the initial reading in of the file into a 2d array, but I seem to be doing something wrong in my output of the data.
I made an add method, and when running the program and calling the method I only get one column of strings and listed in reverse order, but if I do a System.out.println() I don't the output I desire. I am still fairly new to this so I'm sure I just don't see the simple error, but to me, it looks correct.
the reading in of the file
try {
Scanner fileScanner = new Scanner(toOpen);
while (fileScanner.hasNext()) {
fromFile = fileScanner.nextLine();
String temp[] = fromFile.split(" ");
theList.add(temp[0]);
System.out.println(fromFile);
String[][] arr = new String[20][20];
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[i];
System.out.print(arr);
}
}
System.out.println();
}
fileScanner.close();
my add method
public void add(String tableValue) { // Adds a new node
Node newNode = new Node(tableValue);
if (isEmpty()) {
setRoot(newNode);
} else {
newNode.setNext(getRoot());
setRoot(newNode);
}
}
and my method that prints the result
public String makeString() { // A Class that makes a string
String theString = new String();
if (isEmpty()) {
theString = "List is empty";
} else {
Node printer = getRoot();
while (printer != null) {
theString += printer.getTableValue() + " ";
printer = printer.getNext();
}
}
return theString;
}
I guess your problem is here:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[i];
This assigns the same value (temp[i]) to all slots in arr[i]. Again guessing, I think you need something like:
int tmpIndex = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[tmpIndex];
tmpIndex++;
In other words: you have 400 different values in temp. But your code is only assigning the first 20 values 20 times again and again.
Beyond that: System.out.print(arr); isn't doing what you expect it to do - to learn how to print arrays correctly, see this.
As we don't know the number of lines in a file, we can start by storing each line into an ArrayList (of String[]) and then convert it into a 2D array, e.g.:
List<String[]> lines = new ArrayList<>();
while (fileScanner.hasNext()) {
String line = fileScanner.nextLine();
String temp[] = line.split(" ");
lines.add(temp);
}
Now, convert it into an array:
String[][] array = new String[lines.size()][];
for(int i=0 ; i<lines.size() ; i++){
array[i] = lines.get(i);
}
I hevent seen where you have really used your add and makeString methods, and what is the role of the theList variable.
Also, could you please send your file Content.
any way:
If this is your calling to the add method: theList.add(temp[0]); that means that you are inside an Extended class structure that you have not shown it. but Any way you have not used it to fill the 2d Array in the for loop
the Code here is also error: you insert the same element temp[i] in every line !!!!
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = temp[i];
System.out.print(arr);
}
}
You can use a dynamic structure like ArrayList to fill the Elements...
This is my first time asking a question here, so forgive me if my formatting is a little sloppy.
I need to know how to set all the elements of a 2D Array to the same value, using for loops, with Java.
I was able to get this far:
import java.util.Arrays;
public class TheArray{
public static void main (String[] args){
int[][] pixels = new int[768][1024];
for(int i = 0; i < pixels.length; i++){
for(int j = 0; j < pixels[i].length; j++){
Arrays.fill(pixels[i], 867);
}
}
}
}
However I have no idea if I have done it right. So all I want it to do is make all of the elements of the 2D array be 867.
You don't need to use Arrays.fill if you're already using for loops. Just do:
for(int i = 0; i < pixels.length; i++){
for(int j = 0; j < pixels[i].length; j++){
pixels[i][j] = 867;
}
}
Arrays.fill would be used instead of the inner for loop.
Use the following line inside your loop:
pixels[i][j] = 867;
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('[');
}
}
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]);
}