I have a problem in my code
I'm not being able to add Queue
When I tried to add this code
Queue<Integer> myqu = new LinkedList<>();
for (int i = 0; i <= mySecondArray.length + 1; i++){
myqu.add(mySecondArray[i]);
}
To this code
import java.util.LinkedList;
import java.util.Queue;
import java.util.*;
class test{
static class Queue{
static Stack<Integer> s1 = new Stack<Integer>();
Queue<Integer> myqu = new LinkedList<>();
static int[] intArray = new int[]{ 1,2,3,4,5,6,7,8,9,10 };
static int[] mySecondArray = new int[10];
static void enQueue(){
for (int i = 0; i < intArray.length; i++){
s1.push(intArray[i]);
}
System.out.printf("\nBefore multiply operation\n");
System.out.printf("%s \n",s1);
System.out.printf("\nAfter multiply operation\n");
Integer edit = s1.pop();
System.out.printf("[");
for (int i = 1; i <= s1.size() + 1; i++){
mySecondArray[i] = i * 2;
edit = Integer.valueOf(i) * 2;
System.out.printf("%s",edit);
System.out.printf(", ");
}
System.out.printf("]");
for (int i = 0; i <= mySecondArray.length + 1; i++){
myqu.add(mySecondArray[i]);
}
}
};
public static void main(String[] args)
{
Queue q = new Queue();
q.enQueue();
}
}
It says " error: type Queue does not take parameters
Queue myqu = new LinkedList();"
Could you correct the code, Please
You are geeting this issue because your class name and interface Queue name is same.
Change you class name and this issue will resolve like:
static class SampleQueue{
..your code
}
And change your main method for new name:
public static void main(String[] args)
{
SampleQueue q = new SampleQueue();
q.enQueue();
}
If you're trying to use java.util.Queue, why are you creating your own inner static class called Queue? Rename your Queue class, and it's also declared static, so you can't instantiate it with new
Also you myqu is not declared static, so you need to fix that as well, I renamed it myQueue in the code below as per naming conventions
public class test {
static class QueueUtil {
static Stack<Integer> s1 = new Stack<Integer>();
static Queue<Integer> myQueue = new LinkedList<>();
static int[] intArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
static int[] mySecondArray = new int[10];
static void enQueue() {
for (int value : intArray) {
s1.push(value);
}
System.out.print("\nBefore multiply operation\n");
System.out.printf("%s \n", s1);
System.out.print("\nAfter multiply operation\n");
Integer edit = s1.pop();
System.out.print("[");
for (int i = 1; i <= s1.size() + 1; i++) {
mySecondArray[i] = i * 2;
edit = i * 2;
System.out.printf("%s", edit);
System.out.print(", ");
}
System.out.print("]");
for (int i = 0; i <= mySecondArray.length + 1; i++) {
myQueue.add(mySecondArray[i]);
}
}
}
public static void main(String[] args) {
QueueUtil.enQueue();
}
}
This code should fix your issue that you asked about, but I think you're not going to get the results you're expecting...
I recommend reading up on how, when and why to use static, there are a lot of great posts on here.
Related
I've started learning java some time ago. I'm reading through the Java Foundations book and doing exercises from the book to practice.
Just come across this one "Modify the java program so that it works for the numbers in the range between -25 and 25." and I wonder if you have any different solutions to it or is it really that simple? :)
Here's the original code:
public class BasicArray
{
public static void main(String[] args)
{
final int LIMIT = 15;
final int MULTIPLE = 10;
int[] list = new int[LIMIT];
// Initialize the array values
for(int index = 0; index < LIMIT; index++)
list[index] = index * MULTIPLE;
list[5] = 999; // change one array value
// Print the array values
for(int value : list)
System.out.println(value + "");
}
}
And here's my solution to it:
public class BasicArray
{
public static void main(String[] args)
{
final int LIMIT = 51;
final int MULTIPLE = 1;
int[] list = new int[LIMIT];
// Initialize the array values
for(int index = 0; index < LIMIT; index++)
list[index] = (index - 25) * MULTIPLE;
list[5] = 999; // change one array value
// Print the array values
for(int value : list)
System.out.println(value + "");
}
}
Yes, basically it's really simple exercise.
Regarding to your solution we actually don't need MULTIPLE in code.
public class BasicArray {
public static void main(String[] args) {
final int LIMIT = 51;
int[] list = new int[LIMIT];
// Initialize the array values
for(int index = 0; index < LIMIT; index++) {
list[index] = (index - 25);
}
list[5] = 999; // change one array value
// Print the array values
for(int value : list) {
System.out.println(value + "");
}
}
}
If you are ready for a bit of advanced java, you can try following:
public class BasicArray {
public static void main(String[] args) {
IntStream.rangeClosed(-25, 25)
.forEach(System.out::println);
}
}
Or this if you need to replace one value:
public class BasicArray {
public static void main(String[] args) {
IntStream.rangeClosed(-25, 25)
.forEach(i -> {
if (i == -20) { // change one array value
System.out.println(999);
} else {
System.out.println(i);
}
});
}
}
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] xxx={1,3,5,7,9};
System.out.println(Arrays.toString(krakin(xxx)));
}
public static int[] krakin(int[]x) {
for(int i=x.length-1;i>=0;i--) {
int[]dev=new int[x.length-1];
dev[i]=x[i];
}
return dev[i];
}
I'm writing a method in java that reverses the order of the passed array.
I'm getting an error saying void is not allowed in my main method.
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
int[] xxx={1,3,5,7,9};
System.out.println(krakin(xxx));
}
public static void krakin(int[]x){
for(int i=x.length-1;i>=0;i--){
}
}
Your krakin method has a void return type, which means it returns nothing. Therefore you can't pass it as an argument to System.out.println.
You can change it, for example, to return an int array:
public static int[] krakin(int[]x){
int[] rev = new int[x.length];
...
return rev;
}
Then you could print it in your main:
System.out.println(Arrays.toString(krakin(xxx)));
With additional libraries it can be done within one line.
If we consider pure Java then I would write like this:
public static void main(String[] args) {
int[] xxx = {1,3,5,7,9} ;
int[] reversed = reverseWithStream(xxx);
int[] reversed2 = reverseWithTempArray(xxx);
Arrays.stream(reversed).forEach(System.out::println);
Arrays.stream(reversed2).forEach(System.out::println);
}
private static int[] reverseWithStream(int[] array) {
return Arrays.stream(array)
.boxed()
.sorted(Collections.reverseOrder())
.mapToInt(value -> value)
.toArray();
}
private static int[] reverseWithTempArray(int[] sourceArray) {
int[] tempArray = new int[sourceArray.length];
for (int i = 0; i < tempArray.length; i++) {
tempArray[i] = sourceArray[sourceArray.length - 1 - i];
}
return tempArray;
}
One of the biggest benefits of this one is the fact that the previously created array is not affected.
In case of:
java.util.Arrays#sort(int[])
Or apache commons methods, the previously created array is affected.
public static void main(String[] args) {
int[] numbers = new int [100];
int j = numbers.length ;
for (int i = 0; i <= 99; i++) {
numbers [i] = numbers [i] + j ;
j = j - 1 ;
System.out.println(numbers [i]);
}
public class Lab6 {
public static void main(String[] args) {
int List1[] = new int[10];
List1[0] = 1;
List1[1] = 3;
List1[2] = 4;
List1[3] = 5;
List1[4] = 2;
List1[5] = 6;
List1[6] = 8;
List1[7] = 9;
List1[8] = 2;
List1[9] = 7;
toDisplay(List1);
}
public static void toDisplay (List1){
int i;
for(i=0; i>10; i++){
System.out.print(List[i] + " ");
}
}
}
It will not carry over and recognize my List1 array.
How do I display the List1 array in another method without making it a global?
To pass an int[] to toDisplay (and the loop condition should be < not >). Something like
public static void toDisplay (int[] List1){
int i;
for (i=0; i < List1.length; i++) {
System.out.print(List1[i] + " ");
}
}
Change List1 to int[] List1 in the formal parameters of the toDisplay method. By JCC, also you should rename the next names:
List1 -> list1
toDisplay -> display
What about this?
public static void display(int[] list){
Arrays.stream(list).forEach(System.out::println);
}
This is my code:
import java.util.*;
import unit4.collectionsLib.*;
public class Page255Project_class
{
static Scanner reader = new Scanner (System.in);
/**
* #param args
*/
public static void radixSort(int[] numbers){
Queue[] queues = new Queue[10];
for(int i=0; i<numbers.length; i++){
if(numbers[i]%10 == i)
{
queues[i].insert(numbers[i]);
System.out.println(queues[i]);
}
}
}
public static void main(String[] args)
{
int[] numbers = new int[10];
numbers[0] = 170;
numbers[1] = 45;
numbers[2] = 75;
numbers[3] = 90;
numbers[4] = 2;
numbers[5] = 24;
numbers[6] = 802;
radixSort(numbers);
}
}
And I’m getting this error:
Exception in thread "main" java.lang.NullPointerException
at Page255Project_class.radixSort(Page255Project_class.java:15)
at Page255Project_class.main(Page255Project_class.java:33)
Please help to solve that error
In your function radixSort you have defined array of size 10 holds 10 "empty slots", but you forgot to initialize all of the slots.
The change in your code will be like the following:
public static void radixSort(int[] numbers) {
Queue[] queues = new Queue[10];
Arrays.fill(queues, new Queue()); // Queue is interface you need to implement it
// or you need to choose specific type of
// queues [PriorityQueue, LinkedBlockingQueue, ...]
for(int i = 0; i < numbers.length; i++) {
if(numbers[i] % 10 == i) {
queues[i].insert(numbers[i]);
System.out.println(queues[i]);
}
}
}
I have a class that needs to return multiple data objects of various types, such as an ArrayList<Integer> and arrays of double[]. Since java only allows one object to be returned by a given method, I am trying to bundle the various data objects into an ArrayList. However, there are two problems:
The code I am coming up with is unable to read the type of object in each index of the ArrayList.
Specifically, in ListOfObjects.java below, Eclipse gives me an error message stating Type mismatch: cannot convert from Object to ArrayList<Integer> at the line myAL1=dataHolder.get(0);, followed by similar error messages for the three other get statements that follow it.
I do not know what type to specify as the data type for the ArrayList.
My code is in two files below.
Can anyone show me how to fix it so that these two problems are fixed?
I need to be able to subsequently use myAL1 as an ArrayList, and to use myDBL1, mtDBL2, and myDBL3 as double[].
ListOfObjects.java
import java.util.ArrayList;
public class ListOfObjects {
ArrayList<Integer> myAL1 = new ArrayList<Integer>();
double[] myDBL1 = new double[25];
double[] myDBL2 = new double[25];
double[] myDBL3 = new double[25];
public static void main(String[] args) {
}
public void myMethod() {
AssembleListOfObjects myLOO = new AssembleListOfObjects();
ArrayList dataHolder = myLOO.buildListOfObjects();
myAL1 = dataHolder.get(0);
myDBL1 = dataHolder.get(0);
myDBL2 = dataHolder.get(0);
myDBL3 = dataHolder.get(0);
}
}
AssembleListOfObjects.java
import java.util.ArrayList;
public class AssembleListOfObjects {
ArrayList<Integer> al1 = new ArrayList<Integer>();
double[] dbl1 = new double[25];
double[] dbl2 = new double[25];
double[] dbl3 = new double[25];
public void main(String[] args) {
buildListOfObjects();
}
public ArrayList buildListOfObjects() {
ArrayList ListOfObjects = new ArrayList();
ListOfObjects.add(al1);
ListOfObjects.add(dbl1);
ListOfObjects.add(dbl2);
ListOfObjects.add(dbl3);
return ListOfObjects;
}
}
EDIT:
I re-wrote it your way, and here is what I have so far. It throws a java.lang.NoSuchMethodError: main error unless I add the static modifier everywhere in the code.
When I do add the static modifier everywhere, it prints out arrays of zeros and an empty arraylist.
How would you fix this code so that ListOfObjects is able to output values for each of the arrays/arraylist?
But here is the code:
ListOfObjects.java
import java.util.ArrayList;
public class ListOfObjects {
ArrayList<Integer> myAL1 = new ArrayList<Integer>();
double[] myDBL1 = new double[25];
double[] myDBL2 = new double[25];
double[] myDBL3 = new double[25];
public void main(String[] args) {
myMethod();
}
public void myMethod() {
AssembleListOfObjects myLOO = new AssembleListOfObjects();
myAL1 = myLOO.getAl1();
myDBL1 = myLOO.getDbl1();
myDBL2 = myLOO.getDbl2();
myDBL3 = myLOO.getDbl3();
System.out.print("myAL1 is: (");
for (int l = 0; l < myAL1.size(); l++) {
if (l == 0) {
System.out.print(myAL1.get(l));
} else {
System.out.print(", " + myAL1.get(l));
}
}
System.out.println(")");
System.out.print("myDBL1 is: (");
for (int l = 0; l < myDBL1.length; l++) {
if (l == 0) {
System.out.print(myDBL1[l]);
} else {
System.out.print(", " + myDBL1[l]);
}
}
System.out.println(")");
System.out.print("myDBL2 is: (");
for (int l = 0; l < myDBL2.length; l++) {
if (l == 0) {
System.out.print(myDBL2[l]);
} else {
System.out.print(", " + myDBL2[l]);
}
}
System.out.println(")");
System.out.print("myDBL3 is: (");
for (int l = 0; l < myDBL3.length; l++) {
if (l == 0) {
System.out.print(myDBL3[l]);
} else {
System.out.print(", " + myDBL3[l]);
}
}
System.out.println(")");
}
}
AssembleListOfObjects.java
import java.util.ArrayList;
public class AssembleListOfObjects {
private ArrayList<Integer> al1 = new ArrayList<Integer>();
int mySize = 25;
private double[] dbl1 = new double[mySize];
private double[] dbl2 = new double[mySize];
private double[] dbl3 = new double[mySize];
public void main(String[] args) {
setterMethod();
getAl1();
getDbl1();
getDbl2();
getDbl3();
}
public void setterMethod() {
for (int j = 0; j < mySize; j++) {
// the following lines are placeholders for a complex algorithm
dbl1[j] = j;
dbl2[j] = Math.pow((double) j, 3);
dbl3[j] = Math.cos((double) j);
if ((j % 3) == 0) {
al1.add(j);
}
}
}
public ArrayList<Integer> getAl1() {
return al1;
}
public double[] getDbl1() {
return dbl1;
}
public double[] getDbl2() {
return dbl2;
}
public double[] getDbl3() {
return dbl3;
}
}
It's a very bad design decision to try to return mixed types in an array list and suggests that your design is off. If you're always manipulating the ArrayList of Integer and 3 double arrays, why not put them in a class, here you call AssembleListOfObjects, and give that class public getter or accessor methods to get the ArrayList and to get the 3 double arrays individually? Then if you need a method to manipulate this information and return it, it can simply return an object of this class, and whoever calls the method can extract the information it needs by calling the appropriate getter method.
e.g.,
import java.util.ArrayList;
public class AssembleListOfObjects {
private ArrayList<Integer> al1 = new ArrayList<Integer>();
// Also this can be a 2-dimensional array of double
private double[] dbl1 = new double[25];
private double[] dbl2 = new double[25];
private double[] dbl3 = new double[25];
public ArrayList<Integer> getAl1() {
return al1;
}
public double[] getDbl1() {
return dbl1;
}
public double[] getDbl2() {
return dbl2;
}
public double[] getDbl3() {
return dbl3;
}
// public void setter methods
// and any other data manipulation methods
}
Looking at your newly edited code, I modified it some including adding a static modifier to your main method so it is a try main method, and calling myMethod inside on a new ListOfObjects object since myMethod cannot be called in a static context but must be called off of an appropriate object. I've also a call to myLOO.setterMethod(); from within myMethod:
import java.util.ArrayList;
public class ListOfObjects {
ArrayList<Integer> myAL1 = new ArrayList<Integer>();
double[] myDBL1 = new double[25];
double[] myDBL2 = new double[25];
double[] myDBL3 = new double[25];
// added static to main method
public static void main(String[] args) {
// commented out as this can't be called in a static context, but
// needs to be called on an object
// myMethod();
// created a ListOfObjects object and called myMethod on it
ListOfObjects myListOfObjs = new ListOfObjects();
myListOfObjs.myMethod();
}
public void myMethod() {
AssembleListOfObjects myLOO = new AssembleListOfObjects();
myLOO.setterMethod(); // *** added
myAL1 = myLOO.getAl1();
myDBL1 = myLOO.getDbl1();
myDBL2 = myLOO.getDbl2();
myDBL3 = myLOO.getDbl3();
System.out.print("myAL1 is: (");
for (int l = 0; l < myAL1.size(); l++) {
if (l == 0) {
System.out.print(myAL1.get(l));
} else {
System.out.print(", " + myAL1.get(l));
}
}
System.out.println(")");
System.out.print("myDBL1 is: (");
for (int l = 0; l < myDBL1.length; l++) {
if (l == 0) {
System.out.print(myDBL1[l]);
} else {
System.out.print(", " + myDBL1[l]);
}
}
System.out.println(")");
System.out.print("myDBL2 is: (");
for (int l = 0; l < myDBL2.length; l++) {
if (l == 0) {
System.out.print(myDBL2[l]);
} else {
System.out.print(", " + myDBL2[l]);
}
}
System.out.println(")");
System.out.print("myDBL3 is: (");
for (int l = 0; l < myDBL3.length; l++) {
if (l == 0) {
System.out.print(myDBL3[l]);
} else {
System.out.print(", " + myDBL3[l]);
}
}
;
System.out.println(")");
}
}
class AssembleListOfObjects {
private ArrayList<Integer> al1 = new ArrayList<Integer>();
int mySize = 25;
private double[] dbl1 = new double[mySize];
private double[] dbl2 = new double[mySize];
private double[] dbl3 = new double[mySize];
public void main(String[] args) {
setterMethod();
getAl1();
getDbl1();
getDbl2();
getDbl3();
}
public void setterMethod() {
for (int j = 0; j < mySize; j++) {
// the following lines are placeholders for a complex algorithm
dbl1[j] = j;
dbl2[j] = Math.pow((double) j, 3);
dbl3[j] = Math.cos((double) j);
if ((j % 3) == 0) {
al1.add(j);
}
}
}
public ArrayList<Integer> getAl1() {
return al1;
}
public double[] getDbl1() {
return dbl1;
}
public double[] getDbl2() {
return dbl2;
}
public double[] getDbl3() {
return dbl3;
}
}
The way to get around the compiler errors is to use explicit casts, e.g. like this:
ArrayList dataHolder=myLOO.buildListOfObjects();
myAL1=(ArrayList<Integer>)dataHolder.get(0);
myDBL1=(double[])dataHolder.get(0);
However, as already mentioned, this is bad design and you should probably bundle it in a class or something like this.