Print ArrayList of Arrays in Java - java

I created an ArrayList of arrays, but I don't know how to print them.
Here's what I am trying to do:
int j=0;
for (int i=0; i<aArray.length-1; i++){
exp=new int[100];
if (Character.isDigit(aArray[i])){
if(Character.isDigit(aArray[i])==true && aArray[i-1]=='-'){
exp[j]=Character.getNumericValue(-(aArray[i]));
}
else{
exp[j]=Character.getNumericValue(aArray[i]);
}
System.out.print(exp[j]);
j++;
}
}
System.out.println(" ");
list.add(exp);
}
for (int i=0; i<5; i++){
System.out.println(list.get(i)[i]);
}
All I get is 5 zeros. What am I missing? Practically, I read a .txt file in my program, and all I want to do, is seperate and write the numbers of each in the exp[] arrays and then save them in list (ArrayList of Arrays).

Add a for loop to go through each array in the list too:
for (int i=0; i<5; i++){
int[] tmp = list.get(i);
for (int j=0; j<tmp.length; j++) {
System.out.println(tmp[j]);
}
}
edit: You also need to be adding the array to the arraylist inside the for loop, otherwise you're adding a null array.
for (int i=0; i<aArray.length-1; i++){
exp=new int[100];
if (Character.isDigit(aArray[i])){
if(Character.isDigit(aArray[i])==true && aArray[i-1]=='-'){
exp[j]=Character.getNumericValue(-(aArray[i]));
}
else{
exp[j]=Character.getNumericValue(aArray[i]);
}
System.out.print(exp[j]);
j++;
}
System.out.println(" ");
list.add(exp);
}

Problem solved guys, I just needed to initialize exp array outside "for" loop, place a "while" loop and that's all!
Thank you for your answers!

Related

Different ways to iterate 2 dimensional array

I'm preparing for the Java OCA exam but there are several tricky questions about how iterates a multidimensional array.
So, if I had this array and I wanted to iterate using the for and for-each loops what would all be ways to do it?
I usually used only these three:
int [][]matrix = {{3,4,5},{6,7,8},{9},{10,11,12}};
//First way
for (int [] a : matrix){
for (int i =0; i<a.length;i++){
//code
}
}
//Second way
for (int []a: matrix){
for (int i: a){
//code
}
}
//Third way
for (int i = 0; i<matrix.length; i++) {
for (int j=0; j<matrix[a].length; j++) {
//code
}
}
//Fourth way???
Thanks a lot!
You missed the for, foreach combination:
for (int i = 0; i<matrix.length; i++) {
for (int j: matrix[i]){
// code
}
}
You can also use while loops instead of for. Can you be more specific about the context, so we can help you properly with what you need?

Java: How to insert elements from one array to another in specific sequence(homework)

I have a problem with Arrays.
UPDATE:
I need to insert elements from Array A to Array B, staring with uneven indexes and even ones afterwards. for example: A[0] should become B[10], A1=B[0],A[2]=B[11] etc.
I have updated the code (Thank you all for comments, tips and suggestions!).
for (i=0; i<10; i++){
if (i%2==1)
B[C-1]=A[i];
C++;
}
for (i=0; i<10; i++){
if (i%2==0)
B[C]=A[i];
C++;
}
Now it does fill Array B, but only every other element. See picture: Right now output is like this
How do get the B array to fill in right?
Sorry, if this all sounds stupid, I just started to learn programming.
This is not the most elegant or efficient solution, but I think it's simple enough to understand and follow.
public static void main(String[] args) {
double[] a = {0,1,2,3,4,5,6,7,8,9};
double[] b = new double[a.length];
double[] tempArray = new double[a.length];
int tempCounter = 0;
int indexCounter = 0;
for(int i = 0; i<a.length; i++){
if (a[i] % 2 == 0){ // even
tempArray[tempCounter] = a[i];
tempCounter++;
}else{
b[indexCounter] = a[i];
indexCounter++;
}
}
for(int i = 0; i<tempCounter; i++){
b[indexCounter] = tempArray[i];
indexCounter++;
}
}
Thank you all, after all suggestions, I was able to get it right!
for (i=1; i<20; i++){
if (i%2==1){
B[C]=A[i];
C++;
}
}
for (i=0; i<19; i++){
if (i%2==0){
B[C]=A[i];
C++;
}
}

Save data from loop to array

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());

How would I display the position of an element in an array?

My dilemma is after the user inputs a number, that number is then checked to see if it's in the array, if it is i'll let them know that is in the array along with the position of that said number. I have it to where it prompts user for the number, but after that i get the ArrayIndexOutOfBoundsException error.
Here's what I have so far:
int [] iGrades = new int [30];
System.out.print ("Enter the grades, when you're done input (-1) ");
for (int i=0; i<iGrades.length;i++)
{
iGrades [i]= kb.nextInt();
if (iGrades [i]< 0)
{
break;
}
}
System.out.print ("\nEnter a grade to check if it's in the array");
iVal = kb.nextInt();
for(int i=0; i<=iGrades.length; ++i)
{
if(iVal == (iGrades[i]))
{
found = true;
for(int j=0; j<=iGrades.length; ++j)
{
iGrades[j]=j+1;
}
break;
}
}
if (found == true)
{
System.out.println(iVal + " is in the array at position ");
}
else
{
System.out.println(iVal + " is NOT in the array.");
}
}
Any assistance would be great.
The problem is in your second for loop. This
for(int i=0; i<=iGrades.length; ++i)
Change the <= to <.
The exception says it already. You are checking an index that is not in the bounds of the array any more. Look at the second for loop:
for(int i=0; i<=iGrades.length; ++i)
It runs from 0 to 30. The array only goes from 0 to 29 though. You have to use < instead here:
for(int i=0; i < iGrades.length; i++)
Since arrays are zero-based, this
i<=iGrades.length
will allow i to equal Grades.length, which is one past the last index of the array. Use <.
Remember
Array Index start from 0 to length of array -1
Change the loop accordingly
Hint change <= to <

How to stop printing null in an array

How do I stop my program from printing out the positions of the array that are null, when I'm going through an array and printing the positions out?
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]);
}
It's simple like this, but how do I stop it from printing out the positions that are null?
Thanks in advance for an answer to this simple question...
for(int i=0;i<array.length;i++)
{
if (array[i]!=null) {
System.out.println(array[i]);
}
}
You just use the i of the for loop to check inside the array what the value is.
I hope this is what you needed.
for (int i = 0; i < array.length; i++)
{
if(array[i] != null)
{
System.out.print(array[i]);
}
}

Categories