I would like to know how to create a method which takes an ArrayList of Integers (ArrayList) as a parameter and then display the contents of the ArrayList?
I have some code which generates some random numbers and populates the ArrayList with the results, however I keep having errors flag up in eclipse when attempting to create this particular method.
Here is what I have so far:
public void showArray(ArrayList<Integer> array){
return;
}
I know that it is very basic, but I am unsure exactly how to approach it - could it be something like the following?
public void showArray(ArrayList<Integer> array){
Arrays.toString(array);
}
Any assistance would be greatly appreciated.
Thank you.
I'm assuming this is a learning exercise. I'll give you a few hints:
Your method is named showArray, but an ArrayList<T> is of type List<T>, and is not an array. More specifically it is a list that is implemented by internally using an array. Either change the parameter to be an array or else fix the name of the method.
Use an interface if possible instead of passing a concrete class to make your method more reusable.
Minor point: It may be better to have your method return a String, and display the result outside the method.
Try something like this:
public void printList(List<Integer> array) {
String toPrint = ...;
System.out.println(toPrint);
}
You can use a loop and a StringBuilder to construct the toPrint string.
Is there any reason why System.out.println( array ); wouldn't work for you?
Output will be like:
[1, 2, 3]
If you are looking to print the array items, try
public void showArray(ArrayList<Integer> array){
for(int arrayItem : array)
{
System.out.println(arrayItem);
}
}
This sounds like someone wants us to do their homework. You don't have to return anything if you are just displaying it, and if the method has a void return type. I don't know exactly what you want but is it something along the lines of System.out.println(array.elementAt(index))? then you would need a loop.
Related
I created an array Pages in method List.
Could you please advise how to call out one element Pages[1] when accessing method List() from other classes?
public List() {
String[] Pages = {
"www.cnn.com",
"www.bbc.com",
"www.yahoo.com",
};
Many thanks!
You can't call a localvariable from a method directly. You must return it somehow.
Btw, it looks like you don't understand really well how methods work in Java. You should check it.
Here's an example of what do you want to do:
public String[] listPages(){
String [] pages = {"www.cnn.com", "www.bbc.com", "www.yahoo.com"};
return pages;
}
Later if you want to use one of the elements of the array, you should do something like this:
public static void main(String[] args){
String[] res = listPages();
//now we want to print the element one: www.cnn.com
System.out.println(res[0]);
}
You need to create a class named List and a constructor for that class.
Also there are redundant ;, between the elements of the array.
I've got a function to create syllables for words.
I use it like this: syllables(word1field); - creates List with syllables: aa,bb,cc
and syllables(word2field); - creates List with syllables: dd,ee,ff
And in the result I get dd,ee,ff, but I need aa,bb,cc,dd,ee,ff.
Is there possibility to append second list to first?
You get dd,ee,ff because when you call the same method again, it overrides the first ArrayList that is created.
The best thing you could do, that I can think of, is to make your ArrayList global because currently you just keep getting rid of the previous values and create a new ArrayList with the new values you give it. Try doing something like:
public class MyClass {
private List<String> myArray;
public MyClass() {
myArray = new ArrayList<String>();
}
public void syllables(wordfield) {
// do whatever you need to with wordfield
myArray.add(syllable);
}
I don't know how you've got everything laid out but this is the best solution I can think of.
I have a very simple question.
This below method-
newColumnsPredicate
takes input as String... colNames
Below is the method signature as well-
public static SlicePredicate newColumnsPredicate(String... colNames) {
//Some code
}
And I have below collection of strings that I wanted to use in the above method-
final Collection<String> attributeNames
So I decide to use it something like this-
newColumnsPredicate(attributeNames.toString());
This is the right way to do this? As after I run my program, I don't get any data back so I am suspecting it might be possible the way I have added is wrong.
Can anyone help me with this?
String... is a vararg parameter. It is used to indicate that the parameter should either be an array of Strings, or as many String arguments as you like.
Calling toString() on the collection will merely return one string that combines all of the Strings it contains.
You should instead write something that converts your collection to an array, and then pass that in, like:
attributeNames.toArray(new String[attributeNames.size ()])
No, When you do a attributeNames.toString(), you are passing a single String to the method, with a square bracket around them, like "[a, b, c]", Where as your program expects something like "a", "b", "c".
Wouldn't the toArray()function be useful here?
public void test() {
Collection<String> collection = new HashSet<>();
newColumnsPredicate(collection.toArray(new String[collection.size()]));
}
public static SlicePredicate newColumnsPredicate(String... colNames) {
//stuff
}
EDIT:
Whoops, didn't see the other guy answered in the same way.
i've digging around about the same issue but i couldn't find the same as i had
i want to create an array without declaring the size because i don't know how it will be !
to clear the issue i'd like to give you the code that i'm looking up for
public class t
{
private int x[];
private int counter=0;
public void add(int num)
{
this.x[this.counter] = num;
this.counter++;
}
}
as you see the user could use the add function to add element to the array 10000 times or only once so it's unknown size
Using Java.util.ArrayList or LinkedList is the usual way of doing this. With arrays that's not possible as I know.
Example:
List<Float> unindexedVectors = new ArrayList<Float>();
unindexedVectors.add(2.22f);
unindexedVectors.get(2);
You might be looking for a List? Either LinkedList or ArrayList are good classes to take a look at. You can then call toArray() to get the list as an array.
As others have said, use ArrayList. Here's how:
public class t
{
private List<Integer> x = new ArrayList<Integer>();
public void add(int num)
{
this.x.add(num);
}
}
As you can see, your add method just calls the ArrayList's add method. This is only useful if your variable is private (which it is).
Once the array size is fixed while running the program ,it's size can't be changed further.
So better go for ArrayList while dealing with dynamic arrays.
How about this
private Object element[] = new Object[] {};
I think what you really want is an ArrayList or Vector. Arrays in Java are not like those in Javascript.
Little problem.. i want to add some arrays to my arraylist and later get acces to print them out.
Her is my code..
import java.util.*;
class List
{
private ArrayList<int[]> X;
private int[] list;
public X()
{
X = new ArrayList<int[]>();
}
public void addList(int[] list)
{
this.X.add(list);
}
public void showList(int listNumber)
{
System.out.println(X.get(listNumber));
}
Problem is im not getting my list out, but the int code something ... dunno what it really is..
Your problem is in line System.out.println(X.get(listNumber));. Every java object has method toString(). Arrays also have such method but its implementation shows the array reference. To print the array content either iterate over it and print element-by-element or use utility like Arrays.toString():
System.out.println(java.util.Arrays.toString(X.get(listNumber)));
This is because arrays toString() which is called here prints out array reference, not it's contents.
Use instead custom output:
for (int i : X.get(listNumber))
System.out.print(i + " ");
Or, as AlexR proposed, better use provided JDK method Arrays.toString();
By the way, you don't need to prefix this. everywhere in your class' code.
Iterate over all integer arrays in X and in each item (array) print it elements.
This function will do the job for you.
public void printAll(){
for(int [] item:X){
for(int num :item){
System.out.print(num);
}
System.out.println();
}
}
You need to use the Arrays class from java.util.Arrays:
System.out.println(Arrays.toString(X.get(listNumber)));
Just printing out an array prints out its memory location, so to get the actual values, you have to get each element individually, which is what Arrays.toString() does for you.
You are trying to get the array. If you want to display the results of the array you need to do loop through each value in X.get(listNumber) and display the results of the loop.