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.
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:
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:
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.
This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 6 years ago.
please see the code below.
int[] intArray={1,2,3,4,3,4,5};
List intList=Arrays.asList(intArray);
System.out.println(intList.contains(1));
Above code is returning false.can any one pls explain why it is so?
Arrays.asList converts your primitive array to a List<int[]> whose single element is the array, intList.contains(intArray) would return true, but intList.contains(1) won't.
If you change your int[] array to Integer[], you'll get your expected output - i.e. a List<Integer> that contains the elements of the original array.
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);
}