This question already has answers here:
Getting error on printing array
(4 answers)
Closed 6 years ago.
char[] array = {'h','e','l','l','o','h','i','e',' '};
System.out.println(array+"\t"+array.length);//FIX THIS
It prints :
[C#15db9742 9
But if I change the code to :
char[] array = {'h','e','l','l','o','h','i','e',' '};
System.out.println(array);//FIX THIS
It prints :
hellohie
Why can't I put a tab and a number in concatenation? Because the following code worked fine:
String array1 = new String(array);
System.out.println(array1+'\t'+array1.length());
PrintWriter
It has multiple println methods, when you try to System.out.println(array) it inokves the one with char array as the argument, that's why it prints nicely. When you try to invoke your different version, it doesn't know what you expect it to do with that array, that's why it doesn't print nicely. Tab has nothing to do with that.
You can, for example, do System.out.println(Arrays.toString(array)+"\t"+whatever), or convert it to String before like you did, or a lot of other solutions.
Related
This question already has answers here:
How to use subList()
(6 answers)
Closed 3 years ago.
I want to pass an array as argument to a function without first element.
I came up with this solution, but I'm wondering if there are better ways to it.
List<Integer> numbers = new ArrayList<>();
//... in the meantime numbers is an array that contain 1000 elements;
numbers.remove(0);
myFunction(numbers)
You can use subList(firstElement, lastElement); method.
Here please check javadoc.
This question already has answers here:
A quick and easy way to join array elements with a separator (the opposite of split) in Java [duplicate]
(15 answers)
Best way to convert an ArrayList to a string
(27 answers)
What's the simplest way to print a Java array?
(37 answers)
Java function for arrays like PHP's join()?
(24 answers)
Closed 3 years ago.
I have an ArrayList<String> that is added to periodically. What I want to do is to cast the entire ArrayList to a String without doing a loop. Can anybody tell me is it possible without using loop?
Edited:
So we found some solutions, Like
list.stream().collect(Collectors.joining());
Or
String result = String.join(",", list);
or some others as well. Now Just for getting knowledge I put a question which one is the most optimal way for compiler?
You could make a stream out of your list and collect it using joining collector :
list.stream().collect(Collectors.joining());
You can pass the separator to Collectors::joining method for example :
list.stream().collect(Collectors.joining("-"));
Or you can use String::join method :
String result = String.join(",", list);
where , is the separator.
I Believe you can do
String.join(", ", list);
Hope this helps :)
Given some ArrayList<String> s, all you need to use to get a String representation is s.toString().
The format is the following
[element1, element2, element3]
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 5 years ago.
I'm having a problem when I try to parse a String to a char array.
Here is my code:
line = scan.nextLine();
System.out.println(line);
char line2[] = line.toCharArray();
System.out.println(line2.toString());
So, as you can see, it's a simple code.
The problem is: line contains the string "00000001010010110100100000100000", but when I use "line.toCharArray", my char array receives "[C#7e243eed".
I think it's receiving the line variable adress or something like that.
Could someone help me?
Thanks :D
String to char array conversion is fine, it's Sysout that prints hash of the array object instead of its content. If you want to print the array in user friendly way, use:
System.out.println(Arrays.toString(line2));
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
I'm creating a method for a homework problem that returns the highest value in an array. I'm using a for
loop to input the numbers into the array. Here is what the input code looks like.
int[] array = new int [n];
for(i=0;i<array.length;i++){
array[i]=sc.nextInt();
System.out.println(array);}
When i run the program to try and see if there's anything wrong as a I go along I notice the array not returning the value I input into it. For example
putting the number 12 it returns [I#6c80d028. I'm pretty new to java so I don't really know what's going on with the input. Is there any thing that can be done?
Please use:
System.out.println(Arrays.toString(array));
Please do this
System.out.println(array[i]);
or
System.out.println(Arrays.toString(array));
when you
System.out.println(array);
It prints the address and not the value hence you see a junk characters / not expected value.
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
I'm new to Java but not new to programming. I ran the following code in Eclipse:
int[] intArray = new int[5];
System.out.println(intArray);
and received the following output:
[I#17f7be7b
I'm sure StackOverflow already contains the correct way to create an array in Java. What I'd like to know is: what did I do?
Edit: Sorry... The link above my post isn't a duplicate question, and it doesn't answer my question. TylerAndFriends' link is closer, but I was hoping for an explanation of exactly what I printed. Tyler's linked thread says "the default method is to display the object's class name representation, then "#" followed by its hashcode". Can someone elaborate?
Use this
for (int i : intArray) {
System.out.println(i);
}