This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 1 year ago.
I have the following code:
public static void closestInSubnet(int[][] adjlist, short[][] addrs, int src, short[][] queries) {
// TODO
List<String> address = ToList(addrs);
List<String> quer = ToList(queries);
for (String i : address) {
System.out.println(i);
}
}
public static List<String> ToList(short[][] preList) {
List<String> list = new ArrayList<String>();
for (short[] i : preList) {
list.add(i.toString());
}
return list;
}
I want it to be printing out the lists converted to Strings however I am getting the following in console:
[S#7637f22
[S#4926097b
[S#762efe5d
[S#5d22bbb7
[S#41a4555e
[S#3830f1c0
[S#39ed3c8d
[S#71dac704
[S#123772c4
I'm sure there is a quick fix for this any help would be great. thanks
You are created a list of arrays. You are now looping through the list and attempting to print the list in one go. What you are seeing is the object ID of the arrays.
You need to add an inner loop to print the individual elements of each array.
Related
This question already has answers here:
Common elements in two lists
(15 answers)
Closed 4 months ago.
For this I have to take two list as input from user and then I have to compare it. How to do it in java language.
If you want to find common elements between two string ArrayLists
public static ArrayList<String> commonElements(ArrayList<String> a, ArrayList<String> b){
// Initialize arraylist to return
ArrayList<String> toReturn = new ArrayList<String>();
// Iterate across elements of 'a'
for (String s : a){
// If the element is in 'b'
if (b.contains(s)){
// Add it to 'toReturn'
toReturn.add(s);
}
}
return toReturn;
}
Example
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
ArrayList<String> b = new ArrayList<String>();
a.add("hi");
a.add("bye");
a.add("welcome");
a.add("goodbye");
b.add("hi");
b.add("hello");
b.add("goodbye");
for (String s : commonElements(a, b)){
System.out.println(s);
}
}
Output
hi
goodbye
This question already has answers here:
How to get unique values from array
(13 answers)
Array of unique elements?
(6 answers)
Closed 3 years ago.
Suppose in an array, there is the numbers {1,1,1,1,1,5}
How do I write a program that does not specifically relate to the numbers(ex. no printing {1,5}) that gets rid of all the repeating numbers in this array?
In other words, if the problem was {a,b,c,d,e} and I without me knowing the numbers inside, how do I write a program in Java that gets rid of all the repeating digits?(the example on the very top's answer would be {1,5})
If you want to keep only unique element you can use a set.
// Generic function to convert list to set
public static <T> Set<T> convertListToSet(List<T> list)
{
// create a set from the List
return list.stream().collect(Collectors.toSet());
}
public static void main(String args[])
{
// Create a stream of integers
List<String> list = Arrays.asList("GeeksForGeeks",
"Geeks",
"forGeeks",
"A computer portal",
"for",
"Geeks");
// Print the List
System.out.println("List: " + list);
// Convert List to stream
Set<String> set = convertListToSet(list);
// Print the Set
System.out.println("Set from List: " + set);
}
the example is taken from here.
If you need it to be a list you can convert it back with:
List<String> aList = new ArrayList<String>(set);
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
I have a method in a program that takes an ArrayList objects:
private ArrayList<T> list;
and subsequently can use this method to take all of the items, copy them to a String Array, and then remove the items from the original list.
Here is the method:
public String[] takeAll()
{
if (this.list.size() <= 0)
{
throw new NoSuchElementException("Unable to take elements from empty array list");
}//checks if list is empty
String[] targetArray;
targetArray = list.toArray(new String[list.size()]); //adds the Objects to the String[] targetArray
list.clear();
return targetArray;
}
Main that tests this:
public static void main(String args[])
{
SimpleList a;
a = new SimpleList(Position.FIRST, Position.LAST);
String[] b;
a.list.add("thing1");
a.list.add("thing2");
a.list.add("thing3");
a.list.add("thing4");
b = a.takeAll();
System.out.println("The String Array b contains: " + b);
}
When I run it, it returns something odd:
The String Array b contains: [Ljava.lang.String;#15db9742
What would I have to change to actually print out b where it should display {"thing1", "thing2", "thing3", "thing4"} ?
Try the Arrays.toString() method:
System.out.println("The String Array b contains: " + Arrays.toString(b));
In Java, if you ask it to print an Object like that, it will print the toString() of that object. The easiest way to print the array elements is to run it through a for loop and print each element of array.
This question already has answers here:
How to search in a List of Java object
(7 answers)
Closed 6 years ago.
I am creating a gui that can add,remove and search for a name that a user inputs. I would like to know the code that lets me search for items in the arraylist. Thank you
You can use
Java.util.ArrayList.indexOf(Object)
method it will return the index position of first occurrence of the element in the list.
Or
java.util.ArrayList.Contains(Object o)
The above method will return true if the specified element available in the list.
you can do it using predicate to search in a stream representing you list, here is a simple example :
List<String> listOfUsersName = Arrays.asList("toto", "tata", "joe", "marou", "joana", "johny", "");
String userInputSearch = "jo";
List<String> result =listOfUsersName.stream()
.filter(user -> user.contains(userInputSearch))
.collect(Collectors.toList());
result.forEach(System.out::println);
Since your question did not provide sufficient information, I'll make some things up:
//This is your array list that you want to search in:
private ArrayList<String> names = new ArrayList<>(); // Assume this is full of stuff
//This is the user input
private String userInput;
//This method will be called when the search button is clicked
public void onSearchClick() {
}
This is how you are going to implement the search algorithm:
For each item in the array list, if it contains the search string, add it to the search results.
That makes a lot of sense, doesn't it?
In code, you would write this:
private ArrayList<String> searchInList(ArrayList<String> list, String searchString) {
ArrayList<String> results = new ArrayList<>();
for (item in list) {
if (item.contains(searchString)) {
results.add(item);
}
}
return results;
}
And then in the onSearchClick method, you call the search method and do something with the results.
public void onSearchClick() {
ArrayList<String> searchResults = searchInList(names, userInput);
// display the results or whatever
}
If you want a case-insensitive search, you can change the if statement in the search method to this:
if (item.toLowercase().contains(searchString.toLowercase()))
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In Java, how can I test if an Array contains a certain value?
I'm trying to do a sort of:
for(String s : myArray)
{
if(s is in newArray) //how do you test this? i want something like
//if(newArray.indexOf(s)!=-1)
{
//do nothing
}
else
{
//add to newArray
}
}
can someone please help?
if(newAray.contains(s))
{
//do nothing
}
else
{
//add to newArray
}
You cannot add items to arrays at will, because arrays are of fixed size. You need to convert the array to a list first, then add items to te list, and finally convert the list back to array:
List<String> tmp = new ArrayList<String>(Arrays.asList(newArray));
for(String s : myArray) {
if(!tmp.contains(s)) {
tmp.add(s);
}
}
newArray = tmp.toArray(new String[tmp.size()]);