So here's the deal I was messing around with Java trying to make a programme that would take a given int[] and make another int[] which would be in ascending order...
so here the code:
import java.util.*;
public class Accending_order {
public static void main(String args[]) {
int[] dArray = {
1, 34, 25, 67, 35, 68, 88
};
int[] oArray = new int[200];
int[] countOf = new int[200];
for (int i = 0; i < dArray.length; i++) {
int NumForLoop = dArray[i];
for (int j = 0; j < dArray.length; j++) {
int diff = 0;
if (j != i) diff = NumForLoop - dArray[j];
if (diff < 0) countOf[i]++;
}
for (int k = 0; k < dArray.length; k++) {
oArray[k] = dArray[dArray.length - countOf[k]];
}
for (int i2 = 0; i2 < oArray.length; i2++) {
System.out.print(oArray[i2]);
}
}
}
}
and this the ERROR it's showing :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at Accending_order.main(Accending_order.java:19)
so help.....
the problem is here
oArray[k] = dArray[dArray.length - countOf[k]];
when countOf[k] = 0, you are trying to access dArray[dArray.length] and dArray.length is 7, but your array contains elements at indexes 0..6
Related
I am quite new to Java and in the process of learning Arrays. What I am trying to do here is to subtract the largest with the second largest element in an Array. I don't need any help with the process of the code (whether it is correct of not), as I believe I can figure this out myself once I have the Array printed in the console.
However, what I am having trouble with is invoking the diff method to work on the array I have given. The following is the code:
package com.Practice;
import java.util.Arrays;
public class Main {
public static int diff(int[] a) {
int largest = 0;
int secLargest = 0;
for (int i = 0; i < a.length; i++) {
if (a.length < 2) {
System.out.println("Array less than 2 elements!");
}
for (int j = 0; j < a.length; j++) {
if (a[i] > a[j]) {
a[i] = largest;
largest = a[i];
}
if (a[i] < largest) {
a[i] = secLargest;
}
}
}
return largest - secLargest;
}
public static void main(String[] args) {
int[] arr = {22, 3, 2, 55, 34, 56, 34, 123, 56, 34, 21, 5, 65};
System.out.println("Original Array of numbers = " + Arrays.toString(arr));
// How do get the diff() method to work through the array given above down below?
for (int i = 0; i < arr.length; i++) {
System.out.println(diff(arr[]);
}
}
}
I would appreciate the help.
Thank you.
You should pass the array without the [], and you are missing a ):
for (int i = 0; i < arr.length; i++) {
System.out.println(diff(arr));
}
instead of:
for (int i = 0; i < arr.length; i++) {
System.out.println(diff(arr[]);
}
public static void main(String[] args) {
int[][] arr = {{2, 3, 4}, {3, 4, 5, 2}};
System.out.println(line(arr));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;
}
The eror I'm getting is
----jGRASP exec: java Problem
[I#15db9742
----jGRASP: operation complete.
Every object has a toString() method, and the default method is to display the object's class name representation, then "#" followed by its hashcode. So what you're seeing is the default toString() representation of an int array. To print the data in the array, you can use
System.out.println(java.util.Arrays.toString(line(arr)));
Or you can loop through the array with a for loop like this
int [] res = line(arr);
for(int i=0;i<res.length;i++){
System.out.println(res[i]);
}
import java.util.Arrays;
public class TEst {
public static void main(String[] args) {
int[][] arr = { { 2, 3, 4 }, { 3, 4, 5, 2 } };
System.out.println(Arrays.toString(line(arr)));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;`enter code here`
}
}
As far as i can see it is working:
public static void main(String[] args) {
int[][] arr = {{2, 3, 4}, {3, 4, 5, 2}};
int[] i = line(arr);
Arrays.stream(i).forEach(x -> System.out.println(x + ""));
}
public static int[] line(int[][] arr) {
int size = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
size++;
}
}
int[] array = new int[size];
int place = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
array[place] = arr[i][j];
place++;
}
}
return array;
}
the only thing is when you are writing out your result you get the object reference not the array in plain text.
2
3
4
3
4
5
2
Process finished with exit code 0
I am implementing Selection Sort Algorithm in java using ArrayList.
Algorithm I implemented is correct but I am not getting valid output.
can anyone help me please if I am going wrong with this Arraylist.
code :
import java.util.*;
public class SelectionSort {
public static void main(String[] args) {
ArrayList <Integer> array = new ArrayList<Integer>();
array.add(50);
array.add(30);
array.add(10);
array.add(60);
array.add(40);
System.out.println(array);
selsort(array);
}
private static ArrayList<Integer> selsort(ArrayList<Integer> array){
int i = 0;
int imin = 0;
int len = array.size();
for (i = 0;i <len-1; i++){
imin = i;
for (int j = i+1; j<len; j++) {
if ((Integer) (array.get(j)) < (Integer) (array.get(imin))){
imin = j;
}
Collections.swap(array,i,j);
}
}
System.out.println(array);
return array;
}
}
output :
[50, 30, 10, 60, 40] //before
[40, 60, 10, 30, 50] //after
You are swapping the elements with the wrong indices in the wrong place.
The correct swap is i with imin.
The correct place is outside the inner loop:
private static ArrayList<Integer> selsort(ArrayList<Integer> array){
int i = 0;
int len = array.size();
for (i = 0; i < len - 1; i++) {
int imin = i;
for (int j = i + 1; j < len; j++) {
if (array.get(j) < array.get(imin)) {
imin = j;
}
}
Collections.swap(array,i,imin);
}
System.out.println(array);
return array;
}
[50, 30, 10, 60, 40]
[10, 30, 40, 50, 60]
Please let me know the issue i have in this short code. I tried to follow the algo from the book and convert it into a program but have done some error in the process. Its throwing arrayoutofbounds exception
Algo which i tried to follow is bellow:
countingsort(a,b,k)
1)let c[0..k] be a new array
2)for i =0 to k
3) c[i]=0;
4)for j=1 to a.length
5) c[a[j]]=c[a[j]]+1
6)for i = i to k
c[i] = c[i] + c[i-1]
7)for j=a.length downto 1
b[c[a[j]]]= a[j]
c[a[j]] = c[a[j]]-1
public class CountingSort {
public static void main(String[] args) {
int[] array_A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2};
int[] array_B = new int[array_A.length];
int k = 6;
countingSort(array_A,array_B,k);
//System.out.println(array_B);
}
public static void countingSort(int[] A, int[] B, int k){
int[] C = new int[k+1];
for(int i = 0; i<=k; i++){
C[i] = 0;
}
for(int j = 0; j<A.length; j++){
C[A[j]] = C[A[j]] + 1;
}
for(int i = 1; i<=k; i++){
C[i] = C[i] + C[i-1];
}
for(int j = A.length-1; j>=1; j--){
B[C[A[j]]] = A[j];
C[A[j]] = C[A[j]] - 1;
}
System.out.println(Arrays.toString(B));
}
}
Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at importantPrograms.CountingSort.countingSort(CountingSort.java:22)
at importantPrograms.CountingSort.main(CountingSort.java:11)
Here is the modified code that worked for me.
import java.util.Arrays;
public class CountingSort {
public static void main(String[] args) {
int[] array_A = {6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2};
int[] array_B = new int[array_A.length];
int k = 6;
countingSort(array_A,array_B,k);
System.out.println(Arrays.toString(array_B));
}
public static void countingSort(int[] A, int[] B, int k){
int[] C = new int[k+1];
for(int i = 0; i<=k; i++){
C[i] = 0;
}
for(int j = 0; j<A.length; j++){
C[A[j]] ++;
}
for(int i = 1; i<=k; i++){
C[i] += C[i-1];
}
for(int j = A.length-1; j>=0; j--){
B[--C[A[j]]] = A[j];
}
}
}
Ideally, instead of hard-coding the max value (6), you would find the max programmatically and size your count array accordingly. Such as
In main method
countingSort(array_A,array_B,max(array_A));
Max method
public int max(int[] arr){
int m = 0;
for(int i : arr){
if(i>m){
m = i;
}
}
return m;
}
Change
C[A[j]] = C[A[j] + 1]; to C[A[j]] = C[A[j]] + 1; because you are supposed to count the number of particular number occurrence.
I have no clue what you are doing but there is only a little mistake in your second for loop.
Change j<A.length to j<C.length
I try to convert 1D to 2D array, but I keep getting java.lang.ArrayIndexOutOfBoundsException, and I have tried whatever I could find on the stackoverflow or internet, but I do not understand why I have this issue?
public class Arrayto2DArray {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][4];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j]=0;
System.out.print(b[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try{
b[i][j] = a[i+j*4];
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(b[i][j]);
}
System.out.println();
}
}
}
I kind of know why I get this error and it because of this line
b[i][j] = a[i+j*4];
but I cannot come up any formula better than this.
Consider the second for-loop
Lets say when i = 3 and j= 3
a[i+j*4] evaluates to a[15] which is out of the array
When you declare you 2-d array, you specified int[][] b = new int[4][];, meaning that the first inner for loop for (int j = 0; j < b[i].length; j++) should result in a NullPointerException since b[i].length has no predefined length. Before intering the inner for loop, you should define the size of each b[i] like b[i] = new int[somenumber]
In regards to convert the 1d loop to a 2d, you need to define the rule around spliting it into the 2-d array. Then accordingly the second for loop need modification
EDIT:You modified your code to have an int[4][4] array, which means you have 16 placeholders. Your 1-d array contain only 8 placeholders. It depends on how you want to sort the array, like it can be
b 0 1 2 3
0 0 1 6 83
1 4 5 12 7
2 0 0 0 0
3 0 0 0 0
or any other pattern
Assuming the length of 1-d array is 8 and the total index of 2-d array is 16, the following is more of a general solution:
public static void main(String[] args) {
// TODO code application logic here
int[] a = { 0, 1, 6, 83, 4, 5, 12, 7 };
int[][] b = new int[4][4];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j] = 0;
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try {
if ((j + i * 4) < a.length)
b[i][j] = a[j + i * 4];
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
}
What is wrong is your 2D array is a 4*4, so that means for each index, there will be 4 elements i.e. say values. So there would be a total of 16 values, so you would need a 1D array with 16 elements for the above code to work. Right now you have only 8 values in your 1D array.
I think your code should change like this
public class Arrayto2DArray {
public static void main(String[] args) {
// TODO code application logic here
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][2];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j]=0;
System.out.print(b[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < b[i].length; j++) {
try{
b[i][j] = a[i+j*4];
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
}
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}
Your correct formula should be like.
int[] a = {0,1, 6, 83, 4, 5, 12, 7};
int[][] b = new int[4][2];
int k=0;
for (int i = 0; i < b.length; i++) { // Column of 2D array
for (int j = 0; j < b[0].length; j++) { // Row of 2D array
b[i][j]= a[k++];
System.out.println(b[i][j]);
}
}
Edit:
For general case, If your row is fixed at 4 but column is not fixed than your 2D conversion formula should be like below.
int col = a.length / 4;
int remainder = a.length % 4;
if (remainder > 0) {
col = col + 1; // Get the correct column size.
}
int[][] b = new int[4][col];
int k = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < col; j++) {
if (k < a.length) {
b[i][j] = a[k];
System.out.println(b[i][j]);
}
k++;
}
}