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);
}
Related
This is a portion of the client provided by my prof and I'm not allowed to make changes to it.
public static void print (String title, int [] anArray) {
System.out.print(title + ": ");
for (int i = 0; i < anArray.length; i++) {
System.out.print(anArray[i] + " ");
}
System.out.println();
}
public static void main (String [] args) {
System.out.println("\nTesting constructor");
ScoreList list1 = new ScoreList(13);
System.out.println("\nTesting accessor (getter)");
int[] list1_array = list1.getScores();
System.out.println("\nTesting toString");
System.out.print("list1: " + list1);
System.out.println("\nTesting our print method");
print("list1's array", list1_array);
ScoreList list2 = new ScoreList(list1_array);
System.out.println("\nTesting list1 and list2");
System.out.println("list1: " + list1);
System.out.println("list2: " + list2);
System.out.println("\nTesting equals");
System.out.println("It is " + list1.equals(list2)
+ " that list1 is equal to list2");
if (!list1.equals(list2)) {
System.out.println("Error. The equals method does not work correctly");
System.exit(1);
}
This is a portion of my code that I wrote that will be tested by this client:
int [] scores;
public ScoreList(int size) {
if (size >= 1) {
this.scores = new int [size];
for(int i = 0; i < this.scores.length; i++) {
this.scores[i] = random(100);
}
}
else {
System.out.println("Length of array must be greater than or equal to 1.");
}
}
public ScoreList(int [] size) {
if (size.length >= 1) {
this.scores = new int [size.length];
for(int i = 0; i < this.scores.length; i++) {
this.scores[i] = random(100);
}
}
}
private int random(int randomAmount) {
Random rand = new Random();
int randomNumber = rand.nextInt(randomAmount);
return randomNumber;
}
public int [] getScores() {
int [] temp = new int [scores.length];
for(int i = 0; i < scores.length; i++) {
temp[i] = this.scores[i];
}
return temp;
}
The error here is that list1 and list2 will never be equal because I have 2 constructors, one that accepts int as a parameter and one that accepts int []. They both call random() simultaneously to provide the elements for list1 and list2. To make them equal, I think there should only be one constructor, so random() will only be called once. However, the parameters conflict. You see, according to the client, list1's parameter is 13, an int; list2's parameter is an int[].
This is the instruction I got from my prof on how to create the constructor for this class:
A constructor with just one parameter, the size of this object’s scores array, which must
be ≥ 1. It creates an array of the supplied size and the then fills that array with random
integers between 0 and 100, inclusive.
I don't know exactly what you want, but I think you just create the function creating new array from the other.
The second constructor could be like below.
public ScoreList(int[] array) {
// If you have to check array size, do it in here.
this.scores = new int[array.length];
for(int i=0;i<array.length;i++) {
this.scores[i] = array[i];
}
}
or If there should be only one constructor, please make it as a function.
I have a problem with implementation of merge sort in java. I am looking for the error almost week unfortunately without result. ArrayList at the entrance is the same as the output.
import java.util.ArrayList;
import java.util.Scanner;
public class MergeSort
{
private ArrayList<Integer> basicArrayList = new ArrayList<Integer>();
ArrayList<Integer> arrayListA = new ArrayList<Integer>();
ArrayList<Integer> arrayListB = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
private int firstIndexOfArrayList = 0;
private int lastIndexOfArrayListA;
private int lastIndexOfArrayListB;
public void Scal(ArrayList<Integer> basicArrayList, int p, int q, int r) {
this.firstIndexOfArrayList = p;
this.lastIndexOfArrayListA = q;
this.lastIndexOfArrayListB = r;
int numberOfElementsArrayListA = lastIndexOfArrayListA
- firstIndexOfArrayList + 1;
int numberOfElementsArrayListB = lastIndexOfArrayListB
- lastIndexOfArrayListA;
for (int i = 0; i < numberOfElementsArrayListA; i++) {
arrayListA.set(i, basicArrayList.get(firstIndexOfArrayList + i));
}
for (int j = 0; j < numberOfElementsArrayListB; j++) {
arrayListB.set(j, basicArrayList.get(lastIndexOfArrayListA + j));
}
arrayListA.add(Integer.MAX_VALUE);
arrayListB.add(Integer.MAX_VALUE);
int i = 0;
int j = 0;
for (int k = firstIndexOfArrayList; k <= lastIndexOfArrayListB; k++) {
if (arrayListA.get(i) <= arrayListB.get(j)) {
basicArrayList.set(k, arrayListA.get(i));
i = i + 1;
} else {
basicArrayList.set(k, arrayListB.get(j));
j = j + 1;
}
}
}
public void MergeSort(ArrayList basicArrayList, int p, int r) {
this.firstIndexOfArrayList = p;
this.lastIndexOfArrayListB = r;
if (firstIndexOfArrayList < lastIndexOfArrayListB) {
int lastIndexOfArrayListA = (firstIndexOfArrayList + lastIndexOfArrayListB) / 2;
MergeSort(basicArrayList, firstIndexOfArrayList,
lastIndexOfArrayListA);
MergeSort(basicArrayList, lastIndexOfArrayListA + 1,
lastIndexOfArrayListB);
Scal(basicArrayList, firstIndexOfArrayList,
lastIndexOfArrayListA,
lastIndexOfArrayListB);
}
}
public void setSize() {
System.out.println("Enter the number of elements to sort: ");
this.lastIndexOfArrayListB = input.nextInt();
}
public int getSize() {
return lastIndexOfArrayListB;
}
public void setData() {
System.out.println("Enter the numbers: ");
for (int i = 0; i < lastIndexOfArrayListB; i++) {
int number;
number = input.nextInt();
basicArrayList.add(number);
}
}
public void getTable() {
System.out.println(basicArrayList.toString());
}
public static void main(String[] args) {
MergeSort output = new MergeSort();
output.setSize();
output.setData();
output.MergeSort(output.basicArrayList,
output.firstIndexOfArrayList, (output.getSize() - 1));
output.getTable();
}
}
In terms of fixing your code I had a crack at it and as far as I can tell this seems to work. To do this a lot of your code had to be changed but it does now sort all Integers properly
import java.util.ArrayList;
import java.util.Scanner;
public class MergeSort
{
private ArrayList<Integer> basicArrayList = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
private int numbersToSort;
public void doMergeSort(int firstIndexOfArrayList,int lastIndexOfArrayListB, ArrayList<Integer> arrayList)
{
if(firstIndexOfArrayList<lastIndexOfArrayListB && (lastIndexOfArrayListB-firstIndexOfArrayList)>=1)
{
int mid = (lastIndexOfArrayListB + firstIndexOfArrayList)/2;
doMergeSort(firstIndexOfArrayList, mid, arrayList);
doMergeSort(mid+1, lastIndexOfArrayListB, arrayList);
Scal(firstIndexOfArrayList,mid,lastIndexOfArrayListB, arrayList);
}
}
public void Scal(int firstIndexOfArrayList,int lastIndexOfArrayListA,int lastIndexOfArrayListB, ArrayList<Integer> arrayList)
{
ArrayList<Integer> mergedSortedArray = new ArrayList<Integer>();
int leftIndex = firstIndexOfArrayList;
int rightIndex = lastIndexOfArrayListA+1;
while(leftIndex<=lastIndexOfArrayListA && rightIndex<=lastIndexOfArrayListB)
{
if(arrayList.get(leftIndex)<=arrayList.get(rightIndex))
{
mergedSortedArray.add(arrayList.get(leftIndex));
leftIndex++;
}
else
{
mergedSortedArray.add(arrayList.get(rightIndex));
rightIndex++;
}
}
while(leftIndex<=lastIndexOfArrayListA)
{
mergedSortedArray.add(arrayList.get(leftIndex));
leftIndex++;
}
while(rightIndex<=lastIndexOfArrayListB)
{
mergedSortedArray.add(arrayList.get(rightIndex));
rightIndex++;
}
int i = 0;
int j = firstIndexOfArrayList;
while(i<mergedSortedArray.size())
{
arrayList.set(j, mergedSortedArray.get(i++));
j++;
}
}
public void setSize()
{
System.out.println("Enter the number of elements to sort: ");
this.numbersToSort = input.nextInt();
}
public int getSize()
{
return numbersToSort;
}
public void setData()
{
System.out.println("Enter the numbers: ");
for (int i = 0; i < numbersToSort; i++)
{
int number;
number = input.nextInt();
basicArrayList.add(number);
}
}
public void getTable()
{
System.out.println(basicArrayList.toString());
}
public void runSort(ArrayList<Integer> arrayList)
{
doMergeSort(0, this.numbersToSort-1, arrayList);
}
public static void main(String[] args)
{
MergeSort output = new MergeSort();
output.setSize();
output.setData();
output.runSort(output.basicArrayList);
output.getTable();
}
}
Try this code. The following code takes an ArrayList input and outputs an ArrayList as well so it still works along the same basis of your code. The actual sort is handled in a different class MergeSort and is passes into ForMergeSort. Hope this helps
MergeSort.java
public class MergeSort
{
private int[] array;
private int[] tempMergArr;
private int length;
public void sort(int[] inputArr)
{
}
public int[] getSortedArray(int[] inputArr)
{
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);
for(int i=0;i<length;i++)
{
int correctNumber = i+1;
System.out.println("Value "+correctNumber+" of the sorted array which was sorted via the Merge Sort is: "+inputArr[i]);
}
return inputArr;
}
private void doMergeSort(int lowerIndex, int higherIndex)
{
if (lowerIndex < higherIndex)
{
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doMergeSort(lowerIndex, middle);
doMergeSort(middle + 1, higherIndex);
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex)
{
for (int i = lowerIndex; i <= higherIndex; i++)
{
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex)
{
if (tempMergArr[i] <= tempMergArr[j])
{
array[k] = tempMergArr[i];
i++;
}
else
{
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle)
{
array[k] = tempMergArr[i];
k++;
i++;
}
}
}
ForMergeSort.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class ForMergeSort
{
ArrayList<Integer> arrayList = new ArrayList<Integer>();
ArrayList<Integer> sortedArrayList = new ArrayList<Integer>();
MergeSort mS = new MergeSort();
public void buildArrayList()
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of elements to sort: ");
int toSort = input.nextInt();
System.out.println("Enter the numbers: ");
for(int i =0; i<toSort; i++)
{
int number = input.nextInt();
arrayList.add(number);
}
}
public void runMergeSort(ArrayList<Integer> arrayList)
{
int[] arrayOfValues = new int[arrayList.size()];
int i = 0;
for(int a:arrayList)
{
arrayOfValues[i] = a;
i++;
}
MergeSort mS = new MergeSort();
for(int intOfArray:mS.getSortedArray(arrayOfValues))
{
sortedArrayList.add(intOfArray);
}
System.out.println(sortedArrayList.toString());
}
public static void main(String[] args)
{
ForMergeSort fMS = new ForMergeSort();
fMS.buildArrayList();
fMS.runMergeSort(fMS.arrayList);
}
}
I have created the array and outside of its class I have created a method to sort the array. It keeps saying it can't find the variable name of the array I made. When I take the method and put it into the same class as the array it works but it defeats the purpose of what I'm trying to achieve, help?
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Enter a length for the array: ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int randomNumbers[] = new int[x];
for (int index = 0; index < randomNumbers.length; index++)
{
randomNumbers[index] = (int) (Math.random() * 100);
}
for (int index = 0; index < randomNumbers.length; index++)
{
System.out.println(randomNumbers[index]);
}
}
static void sortAscending()
{
Arrays.sort(randomNumbers);
for (int i = 1; i < randomNumbers.length; i++) {
System.out.println("Number: " + randomNumbers[i]);
}
}
Since randomNumbers is declared in the main method, other methods can't access it. There are several ways to make the array accessible from the other method, e.g.:
pass as parameter to the method:
static void sortAscending(int[] randomNumbers) {
//...
}
and call sortAscending call from main like this
sortAscending(randomNumbers);
Pass the value through a field. I wouldn't use a static field however, since there's only one of these fields for all instances. But you could make use a instance of your class and store the value in a non-static field:
publc class MyClass {
// declare randomNumbers as field
private int[] randomNumbers;
public static void main(String[] args) {
MyClass o = new MyClass();
o.localMain(args);
// you could call sortAscending here like this
o.sortAscending();
}
// you don't really need to pass args, since you don't use it
public void localMain(String[] args) {
// TODO code application logic here
System.out.println("Enter a length for the array: ");
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
// assing new value to field
randomNumbers = new int[x];
for (int index = 0; index < randomNumbers.length; index++)
{
randomNumbers[index] = (int) (Math.random() * 100);
}
for (int index = 0; index < randomNumbers.length; index++)
{
System.out.println(randomNumbers[index]);
}
}
void sortAscending()
{
Arrays.sort(randomNumbers);
for (int i = 1; i < randomNumbers.length; i++) {
System.out.println("Number: " + randomNumbers[i]);
}
}
}
I want to create a class that creates a Matrix via an ArrayList.
So that's what I did:
public class Matrice implements IMatrice {
ArrayList elements;
private int numLignes;
private int numColonnes;
public static void main(String[] args) {
Matrice test = new Matrice(3, 4, 6.0);
System.out.println(test);
}
public Matrice (int numLignes, int numColonnes, double valeur){
this.numLignes = numLignes;
this.numColonnes = numColonnes;
elements = new ArrayList(numLignes * numColonnes);
for(int i = 0; i < numLignes * numColonnes; i++){
elements.add(i, valeur);
}
}
}
Now that i created this, I wanted to try if it works. Then I created this toString() method:
public String toString() {
final DecimalFormat DEC_FORMAT = new DecimalFormat("0.0");
final int ESP = 8;
int num;
String sTmp;
String s = "[";
for (int i = 0 ; i < (numLignes * numColonnes) ; i++) {
//etendre i sur ESP colonnes
sTmp = "";
num = ESP - DEC_FORMAT.format(elements.get(i)).length();
for (int j = 0 ; j < num ; j++) {
sTmp = sTmp + " ";
}
sTmp = sTmp + DEC_FORMAT.format(elements.get(i));
if (i != 0 && i % numColonnes == 0) {
s = s + " ]\n[" + sTmp;
} else {
s = s + sTmp;
}
}
s = s + " ]";
return s;
}
Then this is my main to try the Matrix:
public static void main(String[] args) {
Matrice test = new Matrice(3, 4, 6.0);
System.out.println(test);
}
and i don't know why but i only get this :
[ ]
I know that a little thing is wrong but I can't find what. Could you help me?
Okay, i messed up...
The problem was in here :
elements.add(i, valeur);
i did a mistake... i mingled with the set() method.
here is the correction :
elements.add(valeur);
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.